图像上载错误无法打开流:没有此类文件或目录

时间:2018-03-02 04:12:49

标签: php html

我正在尝试上传图片,但我收到以下错误消息,我该如何解决这个问题? 警告:move_uploaded_file(images / homepage2.jpg):无法打开流:第32行的D:\ www \ mdx \ upload_image.php中没有此类文件或目录

警告:move_uploaded_file():无法在第32行的D:\ www \ mdx \ upload_image.php中将'D:\ XAMPP \ tmp \ phpA29E.tmp'移动到'images / homepage2.jpg' 抱歉,上传文件时出错。

 <!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.";
}

2 个答案:

答案 0 :(得分:0)

更改此行:

$target_file = 'images/' . $uploadFileName;

要:

$target_file = 'images/' . basename( $_FILES["imageToUpload"]["name"]);

还要确保您拥有images文件夹。

答案 1 :(得分:0)

请参阅:

//Specify where file will be stored
$target_file = 'images/' . $uploadFileName;

您应该将目标文件更改为绝对路径,或使用您的php脚本文件将其更改为相对路径:

$target_file = './images/' . $uploadFileName;

如果您的代码仍无效,请尝试:

try {
    /* 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>';
    }
} catch (Exception $e) {
    echo $e->getMessage();
}

......知道你得到了什么。