user3201441 user3201441 - 2 months ago 12
PHP Question

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

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
.

Answer

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");
Comments