【发布时间】:2018-08-10 06:57:14
【问题描述】:
我正在尝试上传图片,但在下方收到此错误消息,我该如何解决? 警告:move_uploaded_file(images/homepage2.jpg): failed to open stream: No such file or directory in D:\www\mdx\upload_image.php on line 32
警告:move_uploaded_file(): Unable to move 'D:\XAMPP\tmp\phpA29E.tmp' to 'images/homepage2.jpg' in D:\www\mdx\upload_image.php on line 32 抱歉,上传您的文件时出错。
<!DOCTYPE html>
<html>
<head>
<title>Image Upload Demo</title>
</head>
<body>
<form action="upload_image.php" method="post" enctype="multipart/form-
data">
Select image to upload:
<input type="file" name="imageToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
单独的php文件
<?php
//Check file data has been sent
if(!array_key_exists("imageToUpload", $_FILES)){
echo 'File missing.';
return;
}
if($_FILES["imageToUpload"]["name"] == "" || $_FILES["imageToUpload"]
["name"] == null){
echo 'File missing.';
return;
}
$uploadFileName = $_FILES["imageToUpload"]["name"];
/* Check if image file is a actual image or fake image
tmp_name is the temporary path to the uploaded file. */
if(getimagesize($_FILES["imageToUpload"]["tmp_name"]) === false) {
echo "File is not an image.";
return;
}
// Check that the file is the correct type
$imageFileType = pathinfo($uploadFileName, PATHINFO_EXTENSION);
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType !=
"jpeg" && $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
return;
}
//Specify where file will be stored
$target_file = 'images/' . $uploadFileName;
/* Files are uploaded to a temporary location.
Need to move file to the location that was set earlier in the script */
if (move_uploaded_file($_FILES["imageToUpload"]["tmp_name"], $target_file))
{
echo "The file ". basename( $_FILES["imageToUpload"]["name"]). " has
been uploaded.";
echo '<p>Uploaded image: <img src="' . $target_file . '"></p>';//Include
uploaded image on page
}
else {
echo "Sorry, there was an error uploading your file.";
}
【问题讨论】:
-
D:\www\mdx\images\处真的有写权限的目录吗? -
不确定。我该如何检查?图像、html 和 php 文件都在名为 mdx 的文件夹中,该文件夹位于名为 www 的文件夹中