0

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

任何人都知道任何错误?

4

2 回答 2

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;
}
于 2014-01-21T03:32:04.827 回答
1

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

于 2012-09-10T12:46:18.370 回答