Source code for file /joomla/filesystem/folder.php
Documentation is available at folder.php
* @version $Id: folder.php 6472 2007-02-03 10:47:26Z pasamio $
* @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
* A Folder handling class
* @package Joomla.Framework
* @param string $src The path to the source folder
* @param string $dest The path to the destination folder
* @param string $path An optional base path to prefix to the file names
* @param boolean $force 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
$ftp =
& JFTP::getInstance($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) {
if ($file !=
'.' &&
$file !=
'..') {
if (!@ copy($sfid, $dfid)) {
* Create a folder -- and all necessary parent folders
* @param string $path A path to create from the base path
* @param int $mode 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)) {
// 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
$ftp =
& JFTP::getInstance($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) {
// Return false for JFolder::create because the path to be created is not in open_basedir
if (!$ret =
@mkdir($path, $mode)) {
* @param string $path The path to the folder to delete
* @return boolean True on success
// 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) {
// JFolder::delete throws an error
if ($FTPOptions['enabled'] ==
1) {
// Connect the FTP client
$ftp =
& JFTP::getInstance($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
* @param string $src The path to the source folder
* @param string $dest The path to the destination folder
* @param string $path 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
$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)) {
return JText::_('Rename failed');
return JText::_('Rename failed');
* Wrapper for the standard file_exists function
* @param string $path 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 $path The path of the folder to read
* @param string $filter A filter for file names
* @param boolean $recurse True to recursively search into sub-folders
* @param boolean $fullpath True to return the full path to the file
* @return array Files in the given folder
function files($path, $filter =
'.', $recurse =
false, $fullpath =
false)
// Check to make sure the path valid and clean
// read the source directory
if (($file !=
'.') &&
($file !=
'..') &&
($file !=
'.svn') &&
($file !=
'CVS')) {
* Utility function to read the folders in a folder
* @param string $path The path of the folder to read
* @param string $filter A filter for folder names
* @param boolean $recurse True to recursively search into sub-folders
* @param boolean $fullpath True to return the full path to the folders
* @return array Folders in the given folder
function folders($path, $filter =
'.', $recurse =
false, $fullpath =
false)
// Check to make sure the path valid and clean
// read the source directory
if (($file !=
'.') &&
($file !=
'..') &&
($file !=
'.svn') &&
($file !=
'CVS') &&
$isDir) {
// removes SVN directores from list
* Lists folder in format suitable for tree display
function listFolderTree($path, $filter, $maxLevel =
3, $level =
0, $parent =
0)
$GLOBALS['_JFolder_folder_tree_index'] =
0;
if ($level <
$maxLevel) {
// first path, index foldernames
for ($i =
0, $n =
count($folders); $i <
$n; $i ++
) {
$id = ++
$GLOBALS['_JFolder_folder_tree_index'];
$dirs[] =
array ('id' =>
$id, 'parent' =>
$parent, 'name' =>
$name, 'fullname' =>
$fullName, 'relname' =>
str_replace(JPATH_ROOT, '', $fullName));