递归删除CodeIgniter FTP类中的文件夹


Recursively deleting folder in CodeIgniter FTP Class

我使用的是CI FTP类,函数为delete_dir,它应该删除文件夹及其所有部分,但是,如果文件夹中有文件,它不会删除文件夹并输出错误。

功能如下:;

function delete_dir($filepath)
{
    if ( ! $this->_is_conn())
    {
        return FALSE;
    }
    // Add a trailing slash to the file path if needed
    $filepath = preg_replace("/(.+?)'/*$/", "''1/",  $filepath);
    $list = $this->list_files($filepath);
    if ($list !== FALSE AND count($list) > 0)
    {
        foreach ($list as $item)
        {
            // If we can't delete the item it's probaly a folder so
            // we'll recursively call delete_dir()
            if ( ! @ftp_delete($this->conn_id, $item))
            {
                $this->delete_dir($item);
            }
        }
    }

有人知道有虫子吗?

ftp_delete很可能引发与删除目录无关的错误(例如权限问题)。要显示错误,请删除ftp_delete之前的@

如果您可以使用FTP客户端删除内容和文件夹,那么您也应该能够使用代码删除这些内容和文件夹。尝试修改如下所示的功能

function delete_dir($filepath)
{
    if ( ! $this->_is_conn())
    {
        return FALSE;
    }
    // Add a trailing slash to the file path if needed
    $filepath = preg_replace("/(.+?)'/*$/", "''1/",  $filepath);
    $list = $this->list_files($filepath);
    if ($list !== FALSE AND count($list) > 0)
    {
        foreach ($list as $item)
        {
            // If we can't delete the item it's probaly a folder so
            // we'll recursively call delete_dir()
            if ( ! @ftp_delete($this->conn_id, $filepath.$item))
            {
                $this->delete_dir($filepath.$item);
            }
        }
    }
    $result = @ftp_rmdir($this->conn_id, $filepath);
    if ($result === FALSE)
    {
        if ($this->debug == TRUE)
        {
            $this->_error('ftp_unable_to_delete');
        }
        return FALSE;
    }
    return TRUE;
}