API16:JFolder/delete
From Joomla! Documentation
Description
Delete a folder.
<! removed transcluded page call, red link never existed >
Syntax
delete($path)
| Parameter Name | Default Value | Description |
|---|---|---|
| $path | The path to the folder to delete. |
Returns
boolean True on success.
Defined in
libraries/joomla/filesystem/folder.php
Importing
jimport( 'joomla.filesystem.folder' );
Source Body
function delete($path)
{
// Sanity check
if (!$path) {
// Bad programmer! Bad Bad programmer!
JError::raiseWarning(500, 'JFolder::delete: ' . JText::_('ATTEMPT_TO_DELETE_BASE_DIRECTORY'));
return false;
}
// Initialise 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: ' . $path);
return false;
}
// Remove all the files in folder if they exist; disable all filtering
$files = JFolder::files($path, '.', false, true, array(), array());
if (!empty($files)) {
jimport('joomla.filesystem.file');
if (JFile::delete($files) !== true) {
// JFile::delete throws an error
return false;
}
}
// Remove sub-folders of folder; disable all filtering
$folders = JFolder::folders($path, '.', false, true, array(), array());
foreach ($folders as $folder) {
if (is_link($folder)) {
// Don't descend into linked directories, just delete the link.
jimport('joomla.filesystem.file');
if (JFile::delete($folder) !== true) {
// JFile::delete throws an error
return false;
}
} elseif (JFolder::delete($folder) !== true) {
// JFolder::delete throws an error
return false;
}
}
if ($FTPOptions['enabled'] == 1) {
// Connect the FTP client
jimport('joomla.client.ftp');
$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
if (@rmdir($path)) {
$ret = true;
} elseif ($FTPOptions['enabled'] == 1) {
// Translate path and delete
$path = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $path), '/');
// FTP connector throws an error
$ret = $ftp->delete($path);
} else {
JError::raiseWarning(
'SOME_ERROR_CODE',
'JFolder::delete: ' . JText::_('COULD_NOT_DELETE_FOLDER'),
'Path: ' . $path
);
$ret = false;
}
return $ret;
}
<! removed transcluded page call, red link never existed >
Examples
Code Examples