Back to FileStorage class

Method _deleteFolder

protected bool
_deleteFolder
(mixed $path)
Quickly delete a folder of files
Parameters
  • string $path The path to the folder to delete.
Returns
  • bool
Since
  • 1.7.0
Class: FileStorage
Project: Joomla

Method _deleteFolder - Source code

/**
 * Quickly delete a folder of files
 *
 * @param   string  $path  The path to the folder to delete.
 *
 * @return  boolean
 *
 * @since   1.7.0
 */
protected function _deleteFolder($path)
{
    // Sanity check
    if (!$path || !is_dir($path) || empty($this->_root)) {
        // Bad programmer! Bad, bad programmer!
        Log::add(__METHOD__ . ' ' . Text::_('JLIB_FILESYSTEM_ERROR_DELETE_BASE_DIRECTORY'), Log::WARNING, 'jerror');
        return false;
    }
    $path = $this->_cleanPath($path);
    // Check to make sure path is inside cache folder, we do not want to delete Joomla root!
    $pos = strpos($path, $this->_cleanPath($this->_root));
    if ($pos === false || $pos > 0) {
        Log::add(__METHOD__ . ' ' . 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 = $this->_filesInFolder($path, '.', false, true, array(), array());
    if (!empty($files) && !\is_array($files)) {
        File::invalidateFileCache($files);
        if (@unlink($files) !== true) {
            return false;
        }
    } elseif (!empty($files) && \is_array($files)) {
        foreach ($files as $file) {
            $file = $this->_cleanPath($file);
            // In case of restricted permissions we delete it one way or the other as long as the owner is either the webserver or the ftp
            File::invalidateFileCache($file);
            if (@unlink($file) !== true) {
                Log::add(__METHOD__ . ' ' . Text::sprintf('JLIB_FILESYSTEM_DELETE_FAILED', basename($file)), Log::WARNING, 'jerror');
                return false;
            }
        }
    }
    // Remove sub-folders of folder; disable all filtering
    $folders = $this->_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 (@unlink($folder) !== true) {
                return false;
            }
        } elseif ($this->_deleteFolder($folder) !== true) {
            return false;
        }
    }
    // 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)) {
        return true;
    }
    Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_FOLDER_DELETE', $path), Log::WARNING, 'jerror');
    return false;
}