Actions

API16

JFolder/delete

From Joomla! Documentation

< API16:JFolder
Revision as of 17:47, 22 March 2010 by Doxiki (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

Delete a folder.

[Edit Descripton]

Description:JFolder/delete

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;
}

[Edit See Also] SeeAlso:JFolder/delete

Examples

<CodeExamplesForm />