【问题标题】:Folder delete error - can't delete folders extracted from zip文件夹删除错误 - 无法删除从 zip 中提取的文件夹
【发布时间】:2014-11-18 16:20:23
【问题描述】:

解压后删除服务器中的目录时遇到问题。

我已经将file.zip上传到服务器,然后创建了一个名为“en”的文件,授予它777权限,然后将file.zip的内容提取到“zh”,没关系。 问题是,我现在无法删除en”下的任何文件,但是,重命名似乎可以正常工作。我能够将“en”重命名为“delete_me”并且能够重命名 它也是子目录。

如果我尝试删除“delete_me”下的单个文件,例如 index.html,我会得到

Command:    DELE index.html
Response:   550 index.html: Permission denied

1- FTP 删除尝试:

当我尝试通过 FTP - FileZilla 删除“delete_me”时,它开始在目录中循环并为每个文件显示以下消息:

Response:   150 Opening BINARY mode data connection for MLSD
Response:   226 Transfer complete
Status: Directory listing successful
Command:    DELE index.html
Response:   550 index.html: Permission denied
Command:    CWD /httpdocs/_delete_me
Response:   250 CWD command successful

当我检查(所有者/组)时,我发现它是 (48/48),而我创建的其他目录或文件有 (10618/2524)。

2- cPanel 删除尝试:

我尝试通过主机控制面板文件管理器访问,然后发现“delete_me”中的文件夹的(用户/组)是(apache/apache,而对于我的文件 (/psacln)

当我尝试从主机控制面板中删除文件时,我得到了

Error: Unable to remove file /httpdocs/_delete_me// var/www/vhosts/<hostname>/httpdocs/_delete_me/tmp: filemng failed: rm: cannot remove `/var/www/vhosts/<hostname>/httpdocs/_delete_me/tmp/index.html': Permission denied
filemng: Error occured during /bin/rm command.

3- PHP 删除:

最后一件事我尝试更改权限 @chmod($dirPath, 0777); 并删除 rmdir($dirPath);unlink($file); 但没有结果。

发现与我的问题类似的唯一资源在此处 (Forum Question),但似乎没有得到解答(提供的答案链接不再可用)。

**那么,如何删除文件? **

【问题讨论】:

  • 只能删除空目录。您需要先删除所有内容。
  • 同理,不能删除index.html等个别文件
  • 那是因为权限=)
  • FileZilla 告诉我(权限被拒绝)。有什么解决办法吗?
  • php 用户与 Apache 相同...您上传到的文件夹具有正确的 chmod?试过deathbypenguin2解决方案吗?

标签: php zip extract unlink delete-directory


【解决方案1】:

您必须对该目录中的所有内容进行 chmod。

试试这个

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pathname));

$filemode = "0777"; // Set the permission

// Foreach item found set the permissions
foreach($iterator as $item) {
    chmod($item, $filemode);
}

如果你要做rmdir($dirPath);一定要先从目录中删除所有文件,否则我相信它会删除目录失败。

当然,一旦权限设置为正确的权限,使用unlink($filepath); 删除文件就可以了。

【讨论】:

  • +1 表示递归,但请添加这样一个事实,即可以删除您之前需要的文件夹,以免其中没有文件;)
  • 我创建了一个名为 delete.php 的文件并使用了您提供的代码。我收到以下错误:(Warning: chmod() [function.chmod]: Permission denied in /var/www/vhosts//httpdocs/delete.php on line 17) 删除。 php 已经有一个 777
  • 那是你的 PHP 没有使用 chmod 的权限。这可能是你的问题。检查文件夹的所有权。检查哪个用户正在运行 apache、nginx 或 php。您需要将运行服务器的用户添加到拥有这些文件夹的组中。
  • 如果您可以使用 $this_user = posix_geteuid(); 让用户运行 PHP。然后posix_getpwuid($this_user); 将返回一个信息数组,包括运行 PHP 的用户名。