Source code for file /joomla/filesystem/file.php
Documentation is available at file.php
* @version $Id: file.php 6527 2007-02-08 05:22:52Z tcp $
* @package Joomla.Framework
* @copyright Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
// Check to ensure this file is within the rest of the framework
* @package Joomla.Framework
* Gets the extension of a file name
* @param string $file The file name
* @return string The file extension
* Strips the last extension off 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
$regex =
'#\.\.[^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
* @return boolean True on success
function copy($src, $dest, $path =
null)
// Prepend a base path if it exists
if ($FTPOptions['enabled'] ==
1) {
// Connect the FTP client
$ftp =
& JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $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
// Do NOT use ftp if it is not enabled
if ($FTPOptions['enabled'] ==
1) {
// Connect the FTP client
$ftp =
& JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
foreach ($files as $file) {
// 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
* @return boolean True on success
function move($src, $dest, $path =
'')
return JText::_('Cannot find source file');
if ($FTPOptions['enabled'] ==
1) {
// Connect the FTP client
$ftp =
& JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $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
* @return mixed Returns file contents or boolean False if failed
function read($filename, $incpath =
false)
if (false ===
$fh =
fopen($filename, 'rb', $incpath)) {
$data =
fread($fh, $fsize);
$data .=
fread($fh, 8192);
* Write contents to a file
* @param string $file The full file path
* @param string $buffer The buffer to write
* @return boolean True on success
function write($file, $buffer)
// If the destination directory doesn't exist we need to create it
jimport('joomla.filesystem.folder');
if ($FTPOptions['enabled'] ==
1) {
// Connect the FTP client
$ftp =
& JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $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
* @return boolean True on success
// Ensure that the path is valid and clean
// Create the destination directory if it does not exist
jimport('joomla.filesystem.folder');
if ($FTPOptions['enabled'] ==
1) {
// Connect the FTP client
$ftp =
& JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
//Translate path for the FTP account
// Copy the file to the destination directory
if ($ftp->store($src, $dest)) {
$ftp->chmod($dest, 0777);
* Wrapper for the standard file_exists function
* @param string $file File path
* @return boolean True if path is a file