📄 folder.php
字号:
<?php
/**
* @version $Id: folder.php 8683 2007-08-31 20:02:02Z jinx $
* @package Joomla.Framework
* @subpackage FileSystem
* @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
defined('JPATH_BASE') or die();
jimport('joomla.filesystem.path');
/**
* A Folder handling class
*
* @static
* @author Louis Landry <louis.landry@joomla.org>
* @package Joomla.Framework
* @subpackage FileSystem
* @since 1.5
*/
class JFolder
{
/**
* Copies a folder
*
* @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
* @since 1.5
*/
function copy($src, $dest, $path = '', $force = false)
{
// Initialize variables
jimport('joomla.client.helper');
$FTPOptions = JClientHelper::getCredentials('ftp');
if ($path) {
$src = JPath::clean($path.DS.$src);
$dest = JPath::clean($path.DS.$dest);
}
// Eliminate trailing directory separators, if any
$src = rtrim($src, DS);
$dest = rtrim($dest, DS);
if (!JFolder::exists($src)) {
return JError::raiseError(-1, JText::_('Cannot find source folder'));
}
if (JFolder::exists($dest) && !$force) {
return JError::raiseError(-1, JText::_('Folder already exists'));
}
// Make sure the destination exists
if (! JFolder::create($dest)) {
return JError::raiseError(-1, JText::_('Unable to create target folder'));
}
if ($FTPOptions['enabled'] == 1) {
// Connect the FTP client
jimport('joomla.client.ftp');
$ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
if(! ($dh = @opendir($src))) {
return JError::raiseError(-1, JText::_('Unable to open source folder'));
}
// Walk through the directory copying files and recursing into folders.
while (($file = readdir($dh)) !== false) {
$sfid = $src . DS . $file;
$dfid = $dest . DS . $file;
switch (filetype($sfid)) {
case 'dir':
if ($file != '.' && $file != '..') {
$ret = JFolder::copy($sfid, $dfid, null, $force);
if ($ret !== true) {
return $ret;
}
}
break;
case 'file':
//Translate path for the FTP account
$dfid = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dfid), '/');
if (! $ftp->store($sfid, $dfid)) {
return JError::raiseError(-1, JText::_('Copy failed'));
}
break;
}
}
} else {
if(! ($dh = @opendir($src))) {
return JError::raiseError(-1, JText::_('Unable to open source folder'));
}
// Walk through the directory copying files and recursing into folders.
while (($file = readdir($dh)) !== false) {
$sfid = $src.DS.$file;
$dfid = $dest.DS.$file;
switch (filetype($sfid)) {
case 'dir':
if ($file != '.' && $file != '..') {
$ret = JFolder::copy($sfid, $dfid, null, $force);
if ($ret !== true) {
return $ret;
}
}
break;
case 'file':
if (!@ copy($sfid, $dfid)) {
return JError::raiseError(-1, JText::_('Copy failed'));
}
break;
}
}
}
return true;
}
/**
* 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
* @since 1.5
*/
function create($path = '', $mode = 0755)
{
// Initialize variables
jimport('joomla.client.helper');
$FTPOptions = JClientHelper::getCredentials('ftp');
static $nested = 0;
// 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!
$nested++;
if (($nested > 20) || ($parent == $path)) {
JError::raiseWarning('SOME_ERROR_CODE', 'JFolder::create: '.JText::_('Infinite loop detected'));
$nested--;
return false;
}
// Create the parent directory
if (JFolder::create($parent, $mode) !== true) {
// JFolder::create throws an error
$nested--;
return false;
}
// OK, parent directory has been created
$nested--;
}
// Check if dir already exists
if (JFolder::exists($path)) {
return true;
}
// Check for safe mode
if ($FTPOptions['enabled'] == 1) {
// Connect the FTP client
jimport('joomla.client.ftp');
$ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
// Translate path to FTP path
$path = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $path), '/');
$ret = $ftp->mkdir($path);
$ftp->chmod($path, $mode);
}
else
{
// We need to get and explode the open_basedir paths
$obd = ini_get('open_basedir');
// If open_basedir is set we need to get the open_basedir that the path is in
if ($obd != null)
{
if (JPATH_ISWIN) {
$obdSeparator = ";";
} else {
$obdSeparator = ":";
}
// Create the array of open_basedir paths
$obdArray = explode($obdSeparator, $obd);
$inOBD = false;
// Iterate through open_basedir paths looking for a match
foreach ($obdArray as $test) {
$test = JPath::clean($test);
if (strpos($path, $test) === 0) {
$obdpath = $test;
$inOBD = true;
break;
}
}
if ($inOBD == false) {
// Return false for JFolder::create because the path to be created is not in open_basedir
JError::raiseWarning('SOME_ERROR_CODE', 'JFolder::create: '.JText::_('Path not in open_basedir paths'));
return false;
}
}
// First set umask
$origmask = @ umask(0);
// Create the path
if (!$ret = @mkdir($path, $mode)) {
@ umask($origmask);
JError::raiseWarning('SOME_ERROR_CODE', 'JFolder::create: '.JText::_('Could not create directory'), 'Path: '.$path);
return false;
}
// Reset umask
@ umask($origmask);
}
return $ret;
}
/**
* Delete a folder
*
* @param string $path The path to the folder to delete
* @return boolean True on success
* @since 1.5
*/
function delete($path)
{
// Initialize variables
jimport('joomla.client.helper');
$FTPOptions = JClientHelper::getCredentials('ftp');
// Check to make sure the path valid and clean
$path = JPath::clean($path);
// Is this really a folder?
if (!is_dir($path)) {
JError::raiseWarning(21, 'JFolder::delete: '.JText::_('Path is not a folder').' '.$path);
return false;
}
// Remove all the files in folder if they exist
$files = JFolder::files($path, '.', false, true, array());
if (count($files)) {
jimport('joomla.filesystem.file');
if (JFile::delete($files) !== true) {
// JFile::delete throws an error
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -