Back to Folder class

Method delete

public static bool
delete
(mixed $path)
Delete a folder.
Parameters
  • string $path The path to the folder to delete.
Returns
  • bool True on success.
Since
  • 1.7.0
Class: Folder
Project: Joomla

Method delete - Source code

/**
 * Delete a folder.
 *
 * @param   string  $path  The path to the folder to delete.
 *
 * @return  boolean  True on success.
 *
 * @since   1.7.0
 */
public static function delete($path)
{
    @set_time_limit(ini_get('max_execution_time'));
    // Sanity check
    if (!$path) {
        // Bad programmer! Bad Bad programmer!
        Log::add(__METHOD__ . ': ' . Text::_('JLIB_FILESYSTEM_ERROR_DELETE_BASE_DIRECTORY'), Log::WARNING, 'jerror');
        return false;
    }
    $FTPOptions = ClientHelper::getCredentials('ftp');
    // Check to make sure the path valid and clean
    $path = Path::clean($path);
    // Is this really a folder?
    if (!is_dir($path)) {
        Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_PATH_IS_NOT_A_FOLDER', __METHOD__, $path), Log::WARNING, 'jerror');
        return false;
    }
    // Remove all the files in folder if they exist; disable all filtering
    $files = self::files($path, '.', false, true, array(), array());
    if (!empty($files)) {
        if (File::delete($files) !== true) {
            // File::delete throws an error
            return false;
        }
    }
    // Remove sub-folders of folder; disable all filtering
    $folders = self::folders($path, '.', false, true, array(), array());
    foreach ($folders as $folder) {
        if (is_link($folder)) {
            // Don't descend into linked directories, just delete the link.
            if (File::delete($folder) !== true) {
                // File::delete throws an error
                return false;
            }
        } elseif (self::delete($folder) !== true) {
            // Folder::delete throws an error
            return false;
        }
    }
    if ($FTPOptions['enabled'] == 1) {
        // Connect the FTP client
        $ftp = FtpClient::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $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 = Path::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $path), '/');
        // FTP connector throws an error
        $ret = $ftp->delete($path);
    } else {
        Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_FOLDER_DELETE', $path), Log::WARNING, 'jerror');
        $ret = false;
    }
    return $ret;
}