1

I'm implementing a simple php code to accept uploaded files and store them to the "uploads folder"

I've looked at a few answers on stackexchange including this (which is exactly the error I'm facing): WAMP: failed to open stream: No such file or directory

The error I'm getting is:

Warning: copy(speed.png): failed to open stream: No such file or directory in C:\Program Files\wamp\www\clientservertest\imageupload.php on line 6

My code is as follows:

            <?php
            if( $_FILES['UploadedPic']['name'] != "" )
            {
            $dirPath=dirname(__FILE__).'\uploads\temp.png';

                copy( $_FILES["UploadedPic"]["name"], $dirPath ) or 
                       die( "Could not copy file!$dirPath");
            }
            else
            {
                die("No file specified!");
            }
            ?>

Its also printing the absolute path in $dirPath as:

C:\Program Files\wamp\www\clientservertest\uploads\temp.png

which is absolutely correct.

Really hoping for an answer! :)

EDIT: Code with move_uploaded_file:

            <?php
            if( $_FILES['UploadedPic']['name'] != "" )
            {
            $dirPath=dirname(__FILE__).'\uploads\temp.png';

            move_uploaded_file( $_FILES["UploadedPic"]["name"], $dirPath ) or 
                   die( "Could not copy file!$dirPath");
            }
            else
            {
                die("No file specified!");
            }
            ?>

Error: Could not copy file!C:\Program Files\wamp\www\clientservertest\uploads\temp.png

4
  • Any reason your not using move_uploaded_file? Commented May 3, 2014 at 9:56
  • I actually posted the error for copy since it seemed to be giving more information. I've now included my code with move_uploaded_file and the error. I'm really trying to understand why my implementation is not working whereas the one provided by @Satyam below is. Commented May 3, 2014 at 10:05
  • Its because your passing $_FILES["UploadedPic"]["name"] with is just the name of the file, you need to use $_FILES['UploadedPic']['tmp_name'] which is the actual file location in the tmp folder. Commented May 3, 2014 at 10:08
  • Oh! Amazing. Thanks a lot for the explanation. I'd really appreciate it if you could post it as an answer so I can mark it as the official answer :) Commented May 3, 2014 at 10:10

2 Answers 2

1

You need to to use $_FILES['UploadedPic']['tmp_name'] which is the actual file location in the tmp folder not $_FILES["UploadedPic"]["name"] which is just the name of the file.

Also you need to check that the upload went ok, because there are possible reasons the upload could fail:

$uploaddir = 'uploads/';

// Check for upload attempt
if(isset($_FILES['UploadedPic'])){
    //$newfile = $uploaddir.basename($_FILES['UploadedPic']['name']);
    $newfile = $uploaddir.'temp.png';

    // If no error
    if($_FILES['UploadedPic']['error'] == 0){
        //Attempt to move
        if (move_uploaded_file($_FILES['UploadedPic']['tmp_name'], $newfile)) {
            echo "File was successfully uploaded.";
        }else{
            echo 'Error moving file.';
        }
    } else {
        // Has error
        $errors = array(
            0=>"There is no error, the file uploaded with success",
            1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
            2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
            3=>"The uploaded file was only partially uploaded",
            4=>"No file was uploaded",
            6=>"Missing a temporary folder"
        );
        echo "Error: ".$errors[$_FILES['UploadedPic']['error']];
    }
}

Also you might want to look into checking the upload is actually an image with getimagesize()

Hope it helps good luck.

Sign up to request clarification or add additional context in comments.

Comments

1

Use this code

if($_FILES['UploadedPic']['name'])

    {

    $target='uploads/'.$_FILES['UploadedPic']['name'];

    $source=$_FILES['UploadedPic']['tmp_name'];

    copy($source,$target);

    }

2 Comments

Image Uploading in specific folder
Thanks! This worked. Could you also please explain what was wrong with my code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.