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

Community
  • 1
  • 1
RohanC
  • 301
  • 1
  • 3
  • 12
  • Any reason your not using [move_uploaded_file](http://www.php.net/manual/en/function.move-uploaded-file.php)? – Lawrence Cherone May 03 '14 at 09: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. – RohanC May 03 '14 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. – Lawrence Cherone May 03 '14 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 :) – RohanC May 03 '14 at 10:10

2 Answers2

1

Use this code

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

    {

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

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

    copy($source,$target);

    }
Satyam
  • 60
  • 4
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.

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106