Source for file file.php
Documentation is available at file.php
* @package Joomla.Platform
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* @package Joomla.Platform
* Gets the extension of a file name
* @param string $file The file name
* @return string The file extension
public static function getExt($file)
* Strips the last extension off of a file name
* @param string $file The file name
* @return string The file name without the extension
* Makes file name safe to use
* @param string $file The name of the file [not full path]
* @return string The sanitised string
// Remove any trailing dots, as those aren't ever valid file names.
$file =
rtrim($file, '.');
$regex =
array('#(\.){2,}#', '#[^A-Za-z0-9\.\_\- ]#', '#^\.#');
* @param string $src The path to the source file
* @param string $dest The path to the destination file
* @param string $path An optional base path to prefix to the file names
* @param boolean $use_streams True to use streams
* @return boolean True on success
public static function copy($src, $dest, $path =
null, $use_streams =
false)
// Prepend a base path if it exists
if (!$stream->copy($src, $dest))
JLog::add(JText::sprintf('JLIB_FILESYSTEM_ERROR_JFILE_STREAMS', $src, $dest, $stream->getError()), JLog::WARNING, 'jerror');
if ($FTPOptions['enabled'] ==
1)
// Connect the FTP client
$ftp =
JClientFtp::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
// If the parent folder doesn't exist we must create it
jimport('joomla.filesystem.folder');
// Translate the destination path for the FTP account
if (!$ftp->store($src, $dest))
// FTP connector throws an error
if (!@ copy($src, $dest))
* Delete a file or array of files
* @param mixed $file The file name or an array of file names
* @return boolean True on success
public static function delete($file)
// Do NOT use ftp if it is not enabled
if ($FTPOptions['enabled'] ==
1)
// Connect the FTP client
$ftp =
JClientFtp::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
foreach ($files as $file)
// Try making the file writable first. If it's read-only, it can't be deleted
// on Windows, even if the parent folder is writable
// In case of restricted permissions we zap it one way or the other
// as long as the owner is either the webserver or the ftp
elseif ($FTPOptions['enabled'] ==
1)
if (!$ftp->delete($file))
// FTP connector throws an error
* @param string $src The path to the source file
* @param string $dest The path to the destination file
* @param string $path An optional base path to prefix to the file names
* @param boolean $use_streams True to use streams
* @return boolean True on success
public static function move($src, $dest, $path =
'', $use_streams =
false)
return JText::_('JLIB_FILESYSTEM_CANNOT_FIND_SOURCE_FILE');
if (!$stream->move($src, $dest))
JLog::add(JText::sprintf('JLIB_FILESYSTEM_ERROR_JFILE_MOVE_STREAMS', $stream->getError()), JLog::WARNING, 'jerror');
if ($FTPOptions['enabled'] ==
1)
// Connect the FTP client
$ftp =
JClientFtp::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
// Translate path for the FTP account
// Use FTP rename to simulate move
if (!$ftp->rename($src, $dest))
* Read the contents of a file
* @param string $filename The full file path
* @param boolean $incpath Use include path
* @param integer $amount Amount of file to read
* @param integer $chunksize Size of chunks to read
* @param integer $offset Offset of the file
* @return mixed Returns file contents or boolean False if failed
* @deprecated 13.3 (Platform) & 4.0 (CMS) - Use the native file_get_contents() instead.
public static function read($filename, $incpath =
false, $amount =
0, $chunksize =
8192, $offset =
0)
JLog::add(__METHOD__ .
' is deprecated. Use native file_get_contents() syntax.', JLog::WARNING, 'deprecated');
if ($amount &&
$chunksize >
$amount)
if (false ===
$fh =
fopen($filename, 'rb', $incpath))
if ($amount &&
$fsize >
$amount)
$data =
fread($fh, $amount);
$data =
fread($fh, $fsize);
* 1: Not the end of the file AND
* 2a: No Max Amount set OR
* 2b: The length of the data is less than the max amount we want
while (!feof($fh) &&
(!$amount ||
strlen($data) <
$amount))
$data .=
fread($fh, $chunksize);
* Write contents to a file
* @param string $file The full file path
* @param string &$buffer The buffer to write
* @param boolean $use_streams Use streams
* @return boolean True on success
public static function write($file, &$buffer, $use_streams =
false)
// If the destination directory doesn't exist we need to create it
jimport('joomla.filesystem.folder');
// Beef up the chunk size to a meg
$stream->set('chunksize', (1024 *
1024));
if (!$stream->writeFile($file, $buffer))
JLog::add(JText::sprintf('JLIB_FILESYSTEM_ERROR_WRITE_STREAMS', $file, $stream->getError()), JLog::WARNING, 'jerror');
if ($FTPOptions['enabled'] ==
1)
// Connect the FTP client
$ftp =
JClientFtp::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
// Translate path for the FTP account and use FTP write buffer to file
$ret =
$ftp->write($file, $buffer);
* Moves an uploaded file to a destination folder
* @param string $src The name of the php (temporary) uploaded file
* @param string $dest The path (including filename) to move the uploaded file to
* @param boolean $use_streams True to use streams
* @return boolean True on success
public static function upload($src, $dest, $use_streams =
false)
// Ensure that the path is valid and clean
// Create the destination directory if it does not exist
jimport('joomla.filesystem.folder');
if (!$stream->upload($src, $dest))
if ($FTPOptions['enabled'] ==
1)
// Connect the FTP client
$ftp =
JClientFtp::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
// Translate path for the FTP account
// Copy the file to the destination directory
// Short circuit to prevent file permission errors
* Wrapper for the standard file_exists function
* @param string $file File path
* @return boolean True if path is a file
public static function exists($file)
* Returns the name, without any path.
* @param string $file File path
* @return string filename
* @deprecated 13.3 (Platform) & 4.0 (CMS) - Use basename() instead.
public static function getName($file)
JLog::add(__METHOD__ .
' is deprecated. Use native basename() syntax.', JLog::WARNING, 'deprecated');
// Convert back slashes to forward slashes
return substr($file, $slash +
1);
Documentation generated on Tue, 19 Nov 2013 15:03:03 +0100 by phpDocumentor 1.4.3