Source code for file /joomla/filesystem/folder.php
Documentation is available at folder.php
* @version $Id: folder.php 11675 2009-03-08 20:43:54Z willebil $
* @package Joomla.Framework
* @copyright Copyright (C) 2005 - 2008 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
* A Folder handling class
* @package Joomla.Framework
* @param string The path to the source folder.
* @param string The path to the destination folder.
* @param string An optional base path to prefix to the file names.
* @param boolean Optionally force folder/file overwrites.
* @return mixed JError object on failure or boolean True on success.
function copy($src, $dest, $path =
'', $force =
false)
// Eliminate trailing directory separators, if any
// Make sure the destination exists
if ($ftpOptions['enabled'] ==
1)
// Connect the FTP client
$ftpOptions['host'], $ftpOptions['port'], null,
$ftpOptions['user'], $ftpOptions['pass']
// Walk through the directory copying files and recursing into folders.
while (($file =
readdir($dh)) !==
false) {
$sfid =
$src .
DS .
$file;
$dfid =
$dest .
DS .
$file;
if ($file !=
'.' &&
$file !=
'..') {
// Translate path for the FTP account
if (! $ftp->store($sfid, $dfid)) {
// Walk through the directory copying files and recursing into folders.
while (($file =
readdir($dh)) !==
false) {
$sfid =
$src .
DS .
$file;
$dfid =
$dest .
DS .
$file;
if ($file !=
'.' &&
$file !=
'..') {
if (!@copy($sfid, $dfid)) {
* Create a folder -- and all necessary parent folders.
* @param string A path to create from the base path.
* @param int Directory permissions to set for folders created.
* @return boolean True if successful.
function create($path =
'', $mode =
0755)
// Check to make sure the path valid and clean
$path =
JPath::clean($path);
// Check if parent dir exists
$parent =
dirname($path);
if (!JFolder::exists($parent)) {
// Prevent infinite loops!
if (($nested >
20) ||
($parent ==
$path)) {
'JFolder::create: ' .
JText::_('Infinite loop detected')
// Create the parent directory
// JFolder::create throws an error
// OK, parent directory has been created
// Check if dir already exists
if (JFolder::exists($path)) {
if ($ftpOptions['enabled'] ==
1) {
// Connect the FTP client
$ftpOptions['host'], $ftpOptions['port'], null,
$ftpOptions['user'], $ftpOptions['pass']
// Translate path to FTP path
$ret =
$ftp->mkdir($path);
$ftp->chmod($path, $mode);
// We need to get and explode the open_basedir paths
// If open_basedir is set we need to get the open_basedir that the path is in
// Create the array of open_basedir paths
$obdArray =
explode($obdSeparator, $obd);
// Iterate through open_basedir paths looking for a match
foreach ($obdArray as $test) {
if (strpos($path, $test) ===
0) {
if ($inBaseDir ==
false) {
// Return false for JFolder::create because the path to be created is not in open_basedir
'JFolder::create: ' .
JText::_('Path not in open_basedir paths')
if (!$ret =
@mkdir($path, $mode)) {
'JFolder::create: ' .
JText::_('Could not create directory'),
* @param string The path to the folder to delete.
* @return boolean True on success.
// Bad programmer! Bad Bad programmer!
// Check to make sure the path valid and clean
// Is this really a folder?
// Remove all the files in folder if they exist
// JFile::delete throws an error
// Remove sub-folders of folder
foreach ($folders as $folder) {
// Don't descend into linked directories, just delete the link.
// JFile::delete throws an error
// JFolder::delete throws an error
if ($ftpOptions['enabled'] ==
1) {
// Connect the FTP client
$ftpOptions['host'], $ftpOptions['port'], null,
$ftpOptions['user'], $ftpOptions['pass']
// 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) {
// Translate path and delete
// FTP connector throws an error
'JFolder::delete: ' .
JText::_('Could not delete folder'),
* @param string The path to the source folder.
* @param string The path to the destination folder.
* @param string An optional base path to prefix to the file names.
* @return mixed Error message on false or boolean true on success.
function move($src, $dest, $path =
'')
return JText::_('Cannot find source folder');
return JText::_('Folder already exists');
if ($ftpOptions['enabled'] ==
1) {
// Connect the FTP client
$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)) {
return JText::_('Rename failed');
return JText::_('Rename failed');
* Wrapper for the standard file_exists function
* @param string Folder name relative to installation dir
* @return boolean True if path is a folder
* Utility function to read the files in a folder.
* @param string The path of the folder to read.
* @param string A filter for file names.
* @param mixed True to recursively search into sub-folders, or an
* integer to specify the maximum depth.
* @param boolean True to return the full path to the file.
* @param array Array with names of files which should not be shown in
* @return array Files in the given folder.
function files($path, $filter =
'.', $recurse =
false, $fullpath =
false, $exclude =
array('.svn', 'CVS'))
// Check to make sure the path valid and clean
// read the source directory
while (($file =
readdir($handle)) !==
false)
if (($file !=
'.') &&
($file !=
'..') &&
(!in_array($file, $exclude))) {
$dir =
$path .
DS .
$file;
$arr2 =
JFolder::files($dir, $filter, $recurse -
1, $fullpath);
$arr[] =
$path .
DS .
$file;
* Utility function to read the folders in a folder.
* @param string The path of the folder to read.
* @param string A filter for folder names.
* @param mixed True to recursively search into sub-folders, or an
* integer to specify the maximum depth.
* @param boolean True to return the full path to the folders.
* @param array Array with names of folders which should not be shown in
* @return array Folders in the given folder.
function folders($path, $filter =
'.', $recurse =
false, $fullpath =
false, $exclude =
array('.svn', 'CVS'))
// Check to make sure the path valid and clean
// read the source directory
while (($file =
readdir($handle)) !==
false)
if (($file !=
'.') &&
($file !=
'..') &&
(!in_array($file, $exclude))) {
$dir =
$path .
DS .
$file;
// Removes filtered directories
* Lists folder in format suitable for tree display.
* @param string The path of the folder to read.
* @param string A filter for folder names.
* @param integer The maximum number of levels to recursively read,
* @param integer The current level, optional.
* @param integer Unique identifier of the parent folder, if any.
* @return array Folders in the given folder.
function listFolderTree($path, $filter, $maxLevel =
3, $level =
0, $parent =
0)
$GLOBALS['_JFolder_folder_tree_index'] =
0;
if ($level <
$maxLevel) {
// first path, index foldernames
foreach ($folders as $name) {
$id = ++
$GLOBALS['_JFolder_folder_tree_index'];
* Makes path name safe to use.
* @param string The full path to sanitise.
* @return string The sanitised string.
$ds =
(DS ==
'\\') ?
'\\' .
DS :
DS;
$regex =
array('#[^A-Za-z0-9:\_\-' .
$ds .
' ]#');