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

user3201441

Im trying to upload a csv file then insert its contents into my database. I can successfully upload the file but I am having an error in my insert query. Here's a part of the code:

//Upload the file
$file_path = "uploads/";     
$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
$files = pathinfo($file_path,PATHINFO_EXTENSION);
$uploadstatus   = 1;
$found          = 0;

if ($_SERVER["REQUEST_METHOD"] == "POST") {
// check if the file being submitted is a csv file
$file_type=$_FILES['uploaded_file']['type'];
if ($files=="CSV") {
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
    $uploadstatus = 1;
    echo "<script type='text/javascript'>alert('File upload successful.')</script>";
} else{
   echo "<script type='text/javascript'>alert('File upload failed. Please check then re-upload file.')</script>";
}
}


$filename = $_FILES['uploaded_file']['name'];
if (file_exists($file_path.$filename) && $uploadstatus) {
    if (!$found) {
        include('conn.php');

        //get the file 
        $handle = fopen($filename,"r");

            do { 
                if (isset($data[0])) { 
                    $data0  =   mysql_real_escape_string($data[0]);  //rcode
                    $data1  =   mysql_real_escape_string($data[1]);  //pcode
                    $data2  =   mysql_real_escape_string($data[2]);  //mcode
                    $data3  =   mysql_real_escape_string($data[3]);  //bcode
                    $data4  =   mysql_real_escape_string($data[4]);  //ecode
                    $data5  =   mysql_real_escape_string($data[5]);  //filetype
                    $data6  =   mysql_real_escape_string($data[6]);  //rec_count
                    $data7  =   mysql_real_escape_string($data[7]);  //gs_count
                    $data8  =   mysql_real_escape_string($data[8]);  //be_count
                    $data9  =   mysql_real_escape_string($data[9]);  //qc_count
                    $data10 =   mysql_real_escape_string($data[10]); //tt_count
                    $data11 =   mysql_real_escape_string($data[11]); //rm_count
                    $data12 =   mysql_real_escape_string($data[12]); //sc_count
                    $data13 =   mysql_real_escape_string($data[13]); //in_count
                    $data14 =   mysql_real_escape_string($data[14]); //en_count
                    $data15 =   mysql_real_escape_string($data[15]); //co_count
                    $data16 =   mysql_real_escape_string($data[16]); //ve_count
                    $data17 =   mysql_real_escape_string($data[17]); //editing


                    $query =  "INSERT INTO tbltest (rcode,pcode,mcode,bcode,ecode,filetype,rec_count,gs_count,be_count,qc_count,tt_count,rm_count,sc_count,in_count,en_count,co_count,ve_count,ed_count) 
                               VALUES ('$data0','$data1','$data2','$data3','$data4','$data5','$data6','$data7','$data8','$data9','$data10','$data11','$data12','$data13','$data14','$data15','$data16','$data17') ON DUPLICATE KEY UPDATE rec_count=values(rec_count),gs_count=values(gs_count),be_count=values(be_count),qc_count=values(qc_count),tt_count=values(tt_count),rm_count=values(rm_count),sc_count=values(sc_count),in_count=values(in_count),en_count=values(en_count),co_count=values(co_count),ve_count=values(ve_count),ed_count=values(ed_count)";

                    mysql_query ($query,$connect);
                }     
            } while ($data = fgetcsv($handle,1000,"|")); 
        }   
    }
}

The result of the echo is the correct file, but I get an error that says fopen(): failed to open stream: No such file or directory and fgetcsv() expects parameter 1 to be resource, boolean given.

Eborbob

You're trying to open a file that doesn't exist.

The function move_uploaded_file does exactly what it says - it moves a file that has been uploaded. Once you've called this function the file no longer exists in the temporary upload area.

Change this bit:

$filename = $_FILES['uploaded_file']['name'];
if (file_exists($file_path.$filename) && $uploadstatus) {
    if (!$found) {
        include('conn.php');

        //get the file 
        $handle = fopen($filename,"r");

to:

$filename = $file_path;
if (file_exists($filename) && $uploadstatus) {
    if (!$found) {
        include('conn.php');

        //get the file 
        $handle = fopen($filename,"r");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

Failed to open stream: No such file of directory in... PHP fopen

From Dev

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

From Dev

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

From Dev

PHP - fopen: failed to open stream: Permission denied

From Dev

PHP - failed to open stream error for uploaded image

From Dev

PHP - fopen log_post.txt failed to open stream: no such file or directory

From Dev

file exists but fopen "failed to open stream no such file or directory"

From Dev

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

From Dev

PHP - fopen(): failed to open stream: Permission denied on a mac?

From Dev

php counter, Warning: fopen(clickcount.txt): failed to open stream

From Dev

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

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

move_uploaded_file failed to open stream and Permission denied error

From Dev

move_uploaded_file failed to open stream and Permission denied error

From Dev

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

From Java

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

From Dev

PHP - failed to open stream: No such file or directory in

From Dev

PHP/SQL - failed to open stream: No such file or directory

From Dev

Laravel S3 file upload : fopen(path to file) failed to open stream: Permission denied

From Dev

PHP fopen gives intermittent error failed to open stream: No space left on device

From Dev

File exists but PHP throwing "failed to open stream: no such file or directory"

From Dev

File exists but PHP throwing "failed to open stream: no such file or directory"

From Dev

Warning: fopen failed to open stream: HTTP request failed! HTTP/1.1 406 Not Acceptable in /XAMPP/xamppfiles/htdocs/search.php

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

SwiftMailer 'ErrorException' with message 'fopen(/tmp/...) failed to open stream: Permission denied'

From Dev

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

Related Related

  1. 1

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

  2. 2

    Failed to open stream: No such file of directory in... PHP fopen

  3. 3

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

  4. 4

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

  5. 5

    PHP - fopen: failed to open stream: Permission denied

  6. 6

    PHP - failed to open stream error for uploaded image

  7. 7

    PHP - fopen log_post.txt failed to open stream: no such file or directory

  8. 8

    file exists but fopen "failed to open stream no such file or directory"

  9. 9

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

  10. 10

    PHP - fopen(): failed to open stream: Permission denied on a mac?

  11. 11

    php counter, Warning: fopen(clickcount.txt): failed to open stream

  12. 12

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

  13. 13

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

  14. 14

    move_uploaded_file failed to open stream: Permission denied - Mac

  15. 15

    move_uploaded_file failed to open stream and Permission denied error

  16. 16

    move_uploaded_file failed to open stream and Permission denied error

  17. 17

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

  18. 18

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

  19. 19

    PHP - failed to open stream: No such file or directory in

  20. 20

    PHP/SQL - failed to open stream: No such file or directory

  21. 21

    Laravel S3 file upload : fopen(path to file) failed to open stream: Permission denied

  22. 22

    PHP fopen gives intermittent error failed to open stream: No space left on device

  23. 23

    File exists but PHP throwing "failed to open stream: no such file or directory"

  24. 24

    File exists but PHP throwing "failed to open stream: no such file or directory"

  25. 25

    Warning: fopen failed to open stream: HTTP request failed! HTTP/1.1 406 Not Acceptable in /XAMPP/xamppfiles/htdocs/search.php

  26. 26

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

  27. 27

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

  28. 28

    SwiftMailer 'ErrorException' with message 'fopen(/tmp/...) failed to open stream: Permission denied'

  29. 29

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

HotTag

Archive