简体   繁体   中英

PHP/WAMP: Failed to open stream error with uploaded file

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

Use this code

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

    {

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

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

    copy($source,$target);

    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM