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

RohanC

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

Lawrence Cherone

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

move_uploaded_file failed to open stream and Permission denied error

From Dev

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

From Dev

move_uploaded_file failed to open stream and Permission denied error

From Dev

PHP - fopen(): Failed to Open Stream on uploaded file

From Dev

PHP - fopen(): Failed to Open Stream on uploaded file

From Dev

PHP - failed to open stream error for uploaded image

From Dev

php file upload error warning move_uploaded_file failed to open stream permission denied in

From Dev

move_uploaded_file(...): failed to open stream: No such file or directory

From Dev

move_uploaded_file failed to open stream: Permission denied - Mac

From Dev

SplFileObject error failed to open stream: No such file or directory

From Dev

PHPUnit error "Failed to open stream: No such file or directory"

From Dev

PHP/IIS copy()/move_uploaded_file() failed to open stream: Permission denied Warning

From Dev

move_uploaded_file gives “failed to open stream: Permission denied in /var/www/html”

From Dev

Warning: move_uploaded_file(upload/200.jpg): failed to open stream

From Dev

Laravel classloader.php error failed to open stream: No such file or directory

From Dev

failed to open stream error in simplexml_load_file()

From Dev

yii PDO.php error: failed to open stream: No such file or directory

From Dev

How to stop the error message, If JSON file failed to open stream

From Dev

Failed to open stream: No such file or directory

From Dev

failed to open stream: No such file or directory

From Dev

PHP error failed to open stream/failed opening

From Dev

PHP error failed to open stream/failed opening

From Dev

php Warning: move_uploaded_file(../images/uploads/tk13.JPG) [function.move-uploaded-file]: failed to open stream: Permission denied

From Dev

file_get_contents() how to fix error "Failed to open stream", "No such file"

From Dev

Error on including a file - Warning: include(connect.php): failed to open stream: No such file or director

From Dev

Creating Child Theme. "Warning: file_get_contents9():failed to open stream: No such file or directory" Error

From Dev

Copying .jpg file using shell script gives 'Failed to open input stream for file' error

From Java

PHP - Failed to open stream : No such file or directory

From Java

failed to open stream: No such file or directory (Laravel)

Related Related

  1. 1

    move_uploaded_file failed to open stream and Permission denied error

  2. 2

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

  3. 3

    move_uploaded_file failed to open stream and Permission denied error

  4. 4

    PHP - fopen(): Failed to Open Stream on uploaded file

  5. 5

    PHP - fopen(): Failed to Open Stream on uploaded file

  6. 6

    PHP - failed to open stream error for uploaded image

  7. 7

    php file upload error warning move_uploaded_file failed to open stream permission denied in

  8. 8

    move_uploaded_file(...): failed to open stream: No such file or directory

  9. 9

    move_uploaded_file failed to open stream: Permission denied - Mac

  10. 10

    SplFileObject error failed to open stream: No such file or directory

  11. 11

    PHPUnit error "Failed to open stream: No such file or directory"

  12. 12

    PHP/IIS copy()/move_uploaded_file() failed to open stream: Permission denied Warning

  13. 13

    move_uploaded_file gives “failed to open stream: Permission denied in /var/www/html”

  14. 14

    Warning: move_uploaded_file(upload/200.jpg): failed to open stream

  15. 15

    Laravel classloader.php error failed to open stream: No such file or directory

  16. 16

    failed to open stream error in simplexml_load_file()

  17. 17

    yii PDO.php error: failed to open stream: No such file or directory

  18. 18

    How to stop the error message, If JSON file failed to open stream

  19. 19

    Failed to open stream: No such file or directory

  20. 20

    failed to open stream: No such file or directory

  21. 21

    PHP error failed to open stream/failed opening

  22. 22

    PHP error failed to open stream/failed opening

  23. 23

    php Warning: move_uploaded_file(../images/uploads/tk13.JPG) [function.move-uploaded-file]: failed to open stream: Permission denied

  24. 24

    file_get_contents() how to fix error "Failed to open stream", "No such file"

  25. 25

    Error on including a file - Warning: include(connect.php): failed to open stream: No such file or director

  26. 26

    Creating Child Theme. "Warning: file_get_contents9():failed to open stream: No such file or directory" Error

  27. 27

    Copying .jpg file using shell script gives 'Failed to open input stream for file' error

  28. 28

    PHP - Failed to open stream : No such file or directory

  29. 29

    failed to open stream: No such file or directory (Laravel)

HotTag

Archive