PHP-rmdir(权限被拒绝) [英] PHP - rmdir (permission denied)

查看:283
本文介绍了PHP-rmdir(权限被拒绝)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的脚本来创建和删除文件夹,但是当我尝试删除文件夹时,它会弹出并出现错误.

I have an easy script to create and delete a folder, but when I try to delete a folder, it brings up and error.

代码:

<?php
if ($_POST['hidden']) {
$key = "../g_test/uploads";
$new_folder = $_POST['nazevS'];
$new_dir_path = $key."/".$new_folder;
$dir = mkdir($new_dir_path);    
if($dir)
chmod ($new_dir_path, 0777); 
}
if ($_POST['hiddenSS']) {
$key = "../g_test/uploads";
$new_folder = $_POST['nazevS'];
rmdir($key."/".$new_folder);
}
?>

错误消息:

Warning: rmdir(../g_test/uploads/) [function.rmdir]: Permission denied in /home/free/howto.cz/m/mousemys/root/www/g_test/upload.php on line 51

有人知道如何删除该文件夹(希望其中包含所有内容)吗? 另外,如果您看到其他任何改进,那么代码也可以,请随时告诉我. :-)

Does anyone know how to delete the folder (hopefuly with everything inside) ? Also if you see any other improvments, the code could have, feel free to tell me. :-)

谢谢,迈克.

推荐答案

出于这个答案的目的,我将把允许任何和所有上载放在目录中的安全风险放在一边.我知道这是不安全的,但我认为此问题超出了原始问题的范围.

For the purpose of this answer, I will put the security risks of allowing any and all uploads in a directory aside. I know it's not secure, but I feel this issue is outside the scope of the original question.

正如大家所说,这可能是权限问题.但是由于您已经在代码中创建了目录(删除后该目录很可能以同一用户身份运行).怀疑是这样.

As everybody said, it can be a permission problem. But since you've created the directory in your code (which is most likely running as the same user when deleted). It doubt it is that.

要删除目录,您需要确保:

To delete a directory, you need to make sure that:

  1. 您具有适当的权限(正如每个人都指出的那样).

  1. You have proper permissions (as everyone pointed out).

所有目录句柄必须在删除前关闭.
(打开句柄会导致权限被拒绝错误)

All directory handles must be closed prior to deletion.
(leaving handles open can cause Permission denied errors)

目录必须为空. rmdir()仅删除目录,而不删除其中的文件.因此,如果里面还有东西,它就无法完成工作.

The directory must be empty. rmdir() only deletes the directory, not the files inside. So it can't do its job if there is still stuff inside.

要固定数字2,这非常简单.如果您使用的是这样的内容:

To fix number 2, it is extremely simple. If you are using something like this:

$hd = opendir($mydir);

在删除之前关闭手柄:

closedir($hd);

对于数字3,您要执行的操作称为递归删除.您可以使用以下功能来实现此目的:

For number 3, what you want to do is called a recursive delete. You can use the following function to achieve this:

function force_rmdir($path) {
  if (!file_exists($path)) return false;

  if (is_file($path) || is_link($path)) {
    return unlink($path);
  }

  if (is_dir($path)) {
    $path = rtrim($path, DIR_SEPARATOR) . DIR_SEPARATOR;

    $result = true;

    $dir = new DirectoryIterator($path);

    foreach ($dir as $file) {
      if (!$file->isDot()) {
        $result &= force_rmdir($path . $file->getFilename(), false, $sizeErased);
      }
    }

    $result &= rmdir($path);
    return $result;
  }
}

这篇关于PHP-rmdir(权限被拒绝)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆