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

时间:2012-09-10 12:26:16

标签: php codeigniter ftp

我正在使用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);
            }
        }
    }

任何人都知道有任何错误吗?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

如果您可以使用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;
}