特定文件名的 rmdir() 错误?没有权限

发布于 2024-10-14 09:40:53 字数 1823 浏览 5 评论 0原文

首先,让我们解决这些问题:

  • 目录上没有打开的句柄。
  • 目录中没有文件。
  • 将权限 chmod 为 0777 并不能防止该错误。
  • 目录名称正确。

现在,这是我的问题。尝试删除目录时 rmdir() 抛出此错误:

rmdir(098f6bcd4621d373cade4e832627b4f6) [function.rmdir]: 第 124 行的 path\to\administrate.php 中的权限被拒绝

098f6bcd4621d373cade4e832627b4f6 是目录的名称。

这是脚本的相关部分。

if(is_dir($userhash)) :
foreach (new DirectoryIterator($userhash) as $fileInfo) {
    $fileName = $fileInfo->getFilename();
    if($fileInfo->isDot()) continue;

    if(!rename($userhash.'/'.$fileName , 'trashcan/'.$username.'/'.$fileName)) {
        echo '<p class="error">Could not move '.$fileName.'</p>';
        $err = 1;
    }
}
else :
    echo '<p class="error">Unable to delete files! error: 67</p>';
    $err = 1;
endif;

    //JUST TO BE SURE
    chmod('./',0777);
    chmod($userhash,0777);

// RMDIR ONCE THE DIR IS EMPTY.
if(rmdir($userhash))
    echo '<p class="success">Deleted the user directory. The files are in the trash.</p>';
else {
    echo '<p class="error">Could not remove the user directory. Error: 656</p>';
    $err = 1;
}

更新

我在同一目录中手动创建了目录'jake'。我做了 rmdir('jake'); 并且效果很好。现在,我在同一目录中手动创建了一个目录 '098f6bcd4621d373cade4e832627b4f6' 。我做了 rmdir('098f6bcd4621d373cade4e832627b4f6'); 并且出错了!

更新 2

这开始看起来像一些奇怪的 rmdir() bug,尽管看起来不太可能。以下是我创建的目录名称,然后尝试使用 rmdir 删除它;

098f6bcd4621d373cade4e832627b4f6        |  didn't work (quintuple checked)
098f6bcd4621d373cade4e832627b4f7        |  worked
098f6bcd4621d373cade4e832627b4f         |  worked
098f6bcd4621d373cade4e832627b4f66       |  worked

First, let's get these out of the way:

  • There are no open handles on the directory.
  • There are no files in the directory.
  • chmod ing the permissions to 0777 does not prevent the error.
  • the directory name is correct.

Now then, here's my problem. rmdir() is throwing this error when trying to delete the directory:

rmdir(098f6bcd4621d373cade4e832627b4f6) [function.rmdir]: Permission denied in path\to\administrate.php on line 124

098f6bcd4621d373cade4e832627b4f6 is the name of the directory.

Here is the relevant portion of the script.

if(is_dir($userhash)) :
foreach (new DirectoryIterator($userhash) as $fileInfo) {
    $fileName = $fileInfo->getFilename();
    if($fileInfo->isDot()) continue;

    if(!rename($userhash.'/'.$fileName , 'trashcan/'.$username.'/'.$fileName)) {
        echo '<p class="error">Could not move '.$fileName.'</p>';
        $err = 1;
    }
}
else :
    echo '<p class="error">Unable to delete files! error: 67</p>';
    $err = 1;
endif;

    //JUST TO BE SURE
    chmod('./',0777);
    chmod($userhash,0777);

// RMDIR ONCE THE DIR IS EMPTY.
if(rmdir($userhash))
    echo '<p class="success">Deleted the user directory. The files are in the trash.</p>';
else {
    echo '<p class="error">Could not remove the user directory. Error: 656</p>';
    $err = 1;
}

Update

I manually created the dir 'jake' in the same directory. I did rmdir('jake'); and it worked great. Now, I manually created a dir '098f6bcd4621d373cade4e832627b4f6' in the same directory. I did rmdir('098f6bcd4621d373cade4e832627b4f6'); and it errored!

Update 2

This is beginning to look like some weird rmdir() bug, as unlikely as that seems. Here are directory names I've created and then tried to remove with rmdir;

098f6bcd4621d373cade4e832627b4f6        |  didn't work (quintuple checked)
098f6bcd4621d373cade4e832627b4f7        |  worked
098f6bcd4621d373cade4e832627b4f         |  worked
098f6bcd4621d373cade4e832627b4f66       |  worked

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故笙诉离歌 2024-10-21 09:40:53

为了能够删除文件:

  • 用户应该具有对父目录的写入权限
  • ,不应为父目录设置限制删除标志

更新:
关于限制删除标志 - 来自 man chmod

限制删除标志或粘性位

限制删除标志或粘性
bit 是单个位,其
解释取决于文件
类型。对于目录,它可以防止
非特权用户无法删除
或者重命名目录中的文件
除非他们拥有该文件或
目录;这就是所谓的
限制删除标志
目录,通常可以在
世界可写目录,例如 /tmp。
对于一些较旧的常规文件
系统中,该位保存程序的
交换设备上的文本图像,因此
运行时加载速度更快;这
称为粘性位。

可以通过在mode的第一个八进制数字上加1来设置,例如:

chmod 1xxx dirname

UPDATE 2:

执行php的用户是否有chmod父目录的权限?

换句话说,您确定第一个 chmod 调用返回 true 吗?

chmod('./',0777);

In order to be able remove file:

  • user should have write permissions to PARENT directory
  • restricted deletion flag should NOT be set for PARENT directory

UPDATE:
About restricted deletion flag - from man chmod:

RESTRICTED DELETION FLAG OR STICKY BIT

The restricted deletion flag or sticky
bit is a single bit, whose
interpretation depends on the file
type. For directories, it prevents
unprivi‐leged users from removing
or renaming a file in the directory
unless they own the file or the
directory; this is called the
restricted deletion flag for the
directory, and is commonly found on
world-writable directories like /tmp.
For regular files on some older
systems, the bit saves the program's
text image on the swap device so it
will load more quickly when run; this
is called the sticky bit.

You may SET it by adding 1 to the first octal digit in mode, for example:

chmod 1xxx dirname

UPDATE 2:

Does user, under which php is executed, has permissions to chmod parent directory?

In other words, are you sure that first chmod call returns true?

chmod('./',0777);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文