简体   繁体   English

PHP mkdir():权限被拒绝

[英]PHP mkdir():Permission denied

I have a simple PHP/HTML/CSS app that creates a folder for newly registered users.我有一个简单的 PHP/HTML/CSS 应用程序,可以为新注册的用户创建一个文件夹。 It worked great on my test site, and not that I am ready to "go live" I get the "mkdir(): Permission denied" error.它在我的测试站点上运行良好,但并不是说我已准备好“上线”,我收到了“mkdir(): Permission denied”错误。 As far as I know, all settings are the same on both sites and the file permission for the root and uploads folder are set to 755. Everything else is working as expected accept for the code below...据我所知,两个站点上的所有设置都相同,根文件夹和上传文件夹的文件权限设置为 755。其他一切都按预期工作,接受下面的代码...

if (count($errors) == 0) {
        $pword = md5($pword_1);//encrypt the pword before saving in the database
        $rand = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz";
        $rand = str_shuffle($rand);
        $folder = substr($rand, 0, 10);
        $regDate = date("Y-m-d");
        $token = 0;
        $tokenExp = 0;
        $curDir = getcwd();

        if(mkdir($curDir . "/uploads/" . $folder, 0755)){
            $query = "INSERT INTO users (uname, email, pword, folder, regDate, token, tokenExp) VALUES ('$uname', '$email', '$pword', '$folder', '$regDate', '$token', '$tokenExp')";
            mysqli_query($db, $query);
            $_SESSION['uname'] = $uname;
            $_SESSION['success'] = "You are now logged in";
            header('location: index.php');
        }else{
            array_push($errors, "An error occurred creating your account!!!");
        }
    }

As far as I can tell not being able to create the user's folder, I am not able to upload files.据我所知无法创建用户的文件夹,我无法上传文件。 However, while troubleshooting, I when I manually add the folder to the server, I still get the "path not found" error.但是,在进行故障排除时,当我手动将文件夹添加到服务器时,我仍然收到“找不到路径”错误。 Here's the upload file code...这是上传文件的代码...

if(isset($_POST['uploads'])){
    $uname = mysqli_real_escape_string($db, $_POST['uname']);
    $name = $_FILES['file']['name'];
    $size = $_FILES['file']['size'];
    $type = $_FILES['file']['type'];
    $tmp_name = $_FILES['file']['tmp_name'];
    $extension = substr($name, strpos($name, '.') + 1);
    $max_size = 2500000; //bytes

    if(empty($name)) {
        echo "<p class='error'>Please Select a File</p>";
    }else{
        if($extension == "jpg" || $extension == "jpeg" || $extension == "gif" || $extension == "tif" || $extension == "png" || $extension == "pdf"){
            if($extension == $size<=$max_size){
                $getFold = "SELECT * FROM users WHERE uname='$uname'";
                $getFold = mysqli_query($db, $getFold);
                while($for = mysqli_fetch_assoc($getFold)){
                    $folder = $for['folder'];
                }
                $location = "uploads/" . $folder . "/";
                if(move_uploaded_file($tmp_name, $location . $name)){
                    $query = "INSERT INTO `upload` (name, size, type, location, uname, folder) VALUES ('$name', '$size', '$type', '$location', '$uname', '$folder')";
                    $result = mysqli_query($db, $query);
                    if($result){
                        echo "<p class='success'>File has been uploaded successfully!!!</p>";
                    }else{
                        echo "<p class='error'>Failed to upload file information to database!!! Filename already exist!</p>";
                    }               
                }else{
                    echo "<p class='error'>Failed to Upload File</p>";
                }
            }else{
                echo "<p class='error'>File size should be 3MB or less</p>";
            }
        }else{
            echo "<p class='error'>The selected file is not a JPG, JPEG, GIF, TIF, PNG, or PDF file type!!!</p>";
        }
    }
}

You need to change the owner of the folder in which you are trying to make subfolders for your users to:您需要将尝试为用户创建子文件夹的文件夹的所有者更改为:

  1. apache - in case of CentOS server apache - 在 CentOS 服务器的情况下
  2. www-data - in case of Ubuntu server www-data - 如果是 Ubuntu 服务器

You can do it with the following command您可以使用以下命令来完成

sudo chown -R www-data /folder

The -R flag means that it's recursive, so the apache/httpd process that is running the php will own all the subfolders you might have created as well. -R标志意味着它是递归的,因此运行 php 的 apache/httpd 进程也将拥有您可能创建的所有子文件夹。

For more info about this command take a look at this SO post有关此命令的更多信息,请查看此SO 帖子

You create the dir with the permissions 0755 , which means full access for the owner and only read + execute for the others.您创建具有权限0755的目录,这意味着所有者具有完全访问权限,其他人只能读取 + 执行。 Change it to 0777 or 0766 ;将其更改为07770766 full access to anyone or full access to the owner and read+write to anyone.对任何人的完全访问权限或对所有者的完全访问权限以及对任何人的读写权限。
This also applies to the parent folder.这也适用于父文件夹。

Thank you for your help, I followed your advice and it turned out that my host provider prevented the functionality in an obscure "setting" location accessible via the cPanel.感谢您的帮助,我听从了您的建议,结果我的主机提供商阻止了通过 cPanel 访问的模糊“设置”位置中的功能。 Previously (ie on my older test server/account), the accounts were set up automatically to allow read/write access, the newer accounts are set up to as read-only and require the account owner to make the switch via the setting.以前(即在我较旧的测试服务器/帐户上),帐户会自动设置为允许读/写访问,较新的帐户设置为只读,并要求帐户所有者通过设置进行切换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM