Php 在 Linux 中上传无效

Php Upload in Linux not working

我有这个 PHP 上传脚本。我可以找到 JPG,但不会上传图片。我正在尝试在 Linux mint 机器上的本地主机上执行此操作,因此我的代码位于 /var/www/html/uploads。这是我的 upload.php 代码。

<?php
 $target_dir = "uploads/";
 $target_file = $target_dir . basename($_FILES["fileToUpload"]   ["name"]);
 $uploadOk = 1;
 $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
 // Check if image file is a actual image or fake image
 if(isset($_POST["submit"])) {
 $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
 if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
 } else {
    echo "File is not an image.";
     $uploadOk = 0;
  }
 }
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
 }
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" &&          $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  $uploadOk = 0;
  }
 // Check if $uploadOk is set to 0 by an error
 if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
 // if everything is ok, try to upload file
 } else {
 if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],      $target_file)) {
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). "    has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}
?> 

这是我的媒体代码_upload.php

<!DOCTYPE html>
 <html>
  <body>

  <form action="upload.php" method="post" enctype="multipart/form-data">
   Select image to upload:
   <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
 </form>

 </body>
 </html> 

这是我的 apache2 错误日志中的错误消息。我认为我的问题可能是权限或路径问题。非常感谢所有帮助。

PHP 警告:move_uploaded_file(uploads/bedshaper_1021-36.jpg):无法打开流:/var/www/html/uploads/upload.php 中没有此类文件或目录第 38 行,引用者:http://localhost/uploads/media_upload.php

PHP 警告:move_uploaded_file():无法在线将“/tmp/phpXeCU8c”移动到 /var/www/html/uploads/upload.php 中的 'uploads/bedshaper_1021-36.jpg' 38、referer:http://localhost/uploads/media_upload.php

您必须在“/var/www/html/uploads”中创建文件夹 "uploads" 然后它应该可以工作。 您还可以更改

$target_dir = "uploads/";

$target_dir = "";

如果你想把图片保存在“/var/www/html/uploads”中,你应该做一个直接路径。

$target_dir = "var/www/html/uploads";

编辑 由于您的上传脚本位于您所指的上传文件夹中,因此脚本找不到该文件夹​​。

$target_dir = "../uploads"

也可以

/var/www/html/uploads

将您的两个 .php 文件移到此文件夹之外,如果没有阅读下文,它会正常工作。

您无需更改脚本中的任何内容,只需授予对 'uploads' 文件夹的权限即可。刚刚测试过它并且工作正常。

这里有详细的操作说明: enter link description here

我通过将我想要文件的文件夹重命名为产品并重置对产品文件夹和 upload.php 的权限解决了这个问题我更改了 tar_direct = /var/www/html/uploads/products 一切都是工作正常。感谢您的建议问题已解决。