简体   繁体   English

尽管拥有完全权限,PHP无法移动上传的文件?

[英]PHP Unable To Move Uploaded Files Despite Having Full Permissions?

I'm coding a blog-like website with functions like posting and displaying posts correctly. 我正在使用类似博客的网站进行编码,并具有正确发布和显示帖子的功能。 Currently, I'm trying to add an option to include an image in posts by uploading it to a folder in my project folder using PHP's move_uploaded_file function. 当前,我正在尝试通过使用PHP的move_uploaded_file函数将图像上载到项目文件夹中的方法来添加一个将图像包含在帖子中的选项。

I've given the Apache user, _www, read, write and execute permissions via chmod . 我已经通过chmod为Apache用户_www授予了读取,写入和执行权限。 I've also ensured that it has permissions to write, read and execute in the parent directories leading up to the uploads folder itself. 我还确保了它有权在导致上载文件夹本身的父目录中进行写入,读取和执行。

I've even tried transferring full ownership of the project folder to the said user using chown . 我什至尝试使用chown将项目文件夹的完全所有权转让给所述用户。 Yet, I'm getting a PHP Warning that says: 但是,我收到一个PHP警告,内容为:

PHP Warning: move_uploaded_file(/Users/MyUser/Desktop/Project/assets/php/../uploads/2.jpg): failed to open stream: Permission denied in /Users/MyUser/Desktop/Project/assets/php/upload.php PHP警告:move_uploaded_file(/ Users / MyUser / Desktop / Project / assets / php /../ uploads / 2.jpg):无法打开流:在/ Users / MyUser / Desktop / Project / assets / php / upload中拒绝权限.php文件

PHP 的PHP

$target_dir = __DIR__ . "/../uploads/";
var_dump($target_dir);
$file = basename($_FILES["file"]["name"]);
$target_file = $target_dir . $file;
$allowed_extensions = array("jpeg", "jpg", "png");
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));


if(isset($_POST["upload"])) {
  echo $target_file;

  $stmt = $pdo->prepare("INSERT INTO images (imgTitle, imgLocation) VALUES (:imgTitle, :imgLocation)");
  $stmt->execute(array(
    ":imgTitle" => basename($_FILES["file"]["name"]),
    ":imgLocation" => $target_file
  ));

  if ($imageFileType = in_array($imageFileType, $allowed_extensions)) {
    move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
  }
}

HTML 的HTML

<form action="" method="post" enctype="multipart/form-data">
    Select Image File to Upload:
    <p><input type="file" name="file"></p>
    <input type="submit" name="upload" value="Upload">
</form>

Why is this happening and how can I solve this without changing the _www user to my user? 为什么会发生这种情况,如何在不将_www用户更改为我的用户的情况下解决此问题?

After some troubleshooting myself I finally found out the cause of the error. 经过一些故障排除后,我终于找到了错误的原因。 Going to answer my own question as a note and future reference to those who may end of doing the same error as I did. 我将以笔记的形式回答我自己的问题,并在以后提及那些可能会像我一样犯同样错误的人。

The macOS Apache user is NOT the Apache user MAMP uses. macOS Apache用户不是MAMP使用的Apache用户。

The Apache user of MAMP differs from the user that macOS has built-in, which is _www . MAMP的Apache用户不同于macOS内置的_www Do NOT use _www if you're running MAMP, but rather go to /Applications/MAMP/conf/apache/httpd.conf , this is where the user details for MAMP's Apache user is located at. 如果正在运行MAMP,请不要使用_www ,而应转到/Applications/MAMP/conf/apache/httpd.conf ,这是MAMP的Apache用户的用户详细信息所在的位置。

The user will most likely be your user's username, which is unchangeable. 该用户很可能是您用户的用户名,该用户名不可更改。 The only setting you can change is the group of that Apache user. 您唯一可以更改的设置是该Apache用户的组。 I suggest that you use this for building on your localhost , but remember to change to the new Apache user details when you switch to a web host other than localhost . 我建议您使用它在localhost上进行构建,但是当切换到localhost主机以外的Web主机时,请记住更改为新的Apache用户详细信息。

I hope my mistake can save you hours of confusion and some degree of frustration. 我希望我的错误可以节省您数小时的困惑和一定程度的沮丧。 Have fun coding! 祝您编码愉快!

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

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