这是我的背景:
我正在使用运行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);
}
}
?>
&#13;
<!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>
&#13;
感谢阅读,如果有什么不合理的话,请发表评论!
答案 0 :(得分:0)
上传后添加/
更改
$target_dir = "/backend/images/uploads";
以下代码
$target_dir = "/backend/images/uploads/";