FP Jones TheThird
FP Jones TheThird

Reputation: 15

Image upload error failed to open stream: No such file or directory

I am trying to upload an image but i am getting this error message below, how can i fix this? Warning: 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

Warning: 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 Sorry, there was an error uploading your file.

 <!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>

seperate php file

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

Upvotes: 0

Views: 7383

Answers (2)

jqueryy
jqueryy

Reputation: 177

Change this line :

$target_file = 'images/' . $uploadFileName;

To:

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

Also make sure that you have the images folder.

Upvotes: 0

vietanhyt
vietanhyt

Reputation: 668

See:

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

You should change the target file to absolute path, or change it to relative path with your php script file:

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

If your code still does not work, try:

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();
}

... to know what you got.

Upvotes: 0

Related Questions