完成所有步骤后,PHP 文件上传不起作用.(我认为)


PHP File upload not working after completing all the steps. (I think)

这是我的背景:

我使用的是运行 Apache 和 LAMP 的 Amazon EC2 Linux 实例。

我正在尝试上传 PHP 图像。

我从W3Schools获得了代码。

我尝试以Superuser (su)身份登录并将上传文件夹(/var/www/html/photo/backend/images/uploads(的权限更改为chmod 755

我试过运行print_r($_FILES);并得到Array ( ).

我在php.ini中更改了这些内容:

  • file_uploads设置为on
  • max_execution_time设置为 300(秒(
  • max_file_size设置为 100 (MB(

我已经尝试并谷歌了我能想到的所有内容,但我仍然无法弄清楚为什么我的图像无法上传。如果这很宽泛,我很抱歉,但我真的不知道问题是什么。


问题:

图片无法上传(不知道为什么,我想我已经按照所有步骤操作了,见上文(


这是我的代码:

<?php
$target_dir = "/backend/images/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 $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.";
		print_r($_FILES);
    }
}
?>
<!DOCTYPE html>
<html>
<body>
<form action="/backend/images/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>

感谢您的阅读,如果有什么没有意义的地方,请发表评论!

上传

后添加/

改变

$target_dir = "/backend/images/uploads"; 

到下面的代码

$target_dir = "/backend/images/uploads/";