Azlina T
Azlina T

Reputation: 176

fopen() [function.fopen] error

I am developing a system and I got this error when I tried to upload .csv file into database.

Warning: fopen() [function.fopen]: Filename cannot be empty in C:\xampp\htdocs\cubaan\importcsv.php on line 14

which refer to my coding,

<html>

<form name="import" method="post" enctype="multipart/form-data">
<b> Import your .csv (Excel) here</b><br/><br/> 
    <input type="file" name="file"/><br />
    <input type="submit" name="submit" value="Submit" /><br/>
</form>
<?php
include ("connection.php");

if(isset($_POST["submit"]))
{
    $file = $_FILES['file']['tmp_name'];<--line 14
    $handle = fopen($file, "r");
    $c = 0;
    $row = 1;

    while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
    {
    if($row == 1)
        { 
        $row++; continue; 
        }
    $num = count($filesop);
    $id = $filesop[0];
    $name = $filesop[1];
    $address = $filesop[2];
    $contact1 = $filesop[3];
    $contact2 = $filesop[4];
    $department = $filesop[5];
    $lokasi = $filesop[6];


    $sql = mysql_query("INSERT INTO customer_details (id,customer_name,customer_address,customer_contact1,customer_contact2,department,lokasi) VALUES ($id,'$name','$address','$contact1','$contact2','$department','$lokasi')");
    $c = $c + 1;
    }

        if($sql){
            echo "You database has imported successfully. You have inserted ". $c ." records";
        }else{
            echo "Sorry! There is some problem.";
        }
 include ("ex.php");

if(isset($_POST["submit"]))
{
    ExportExcel("customer_details");

}


}
?>

the data contain inside the file is not uploaded into the database. I have look for the same error in the other question but nothing working. The data that I upload book1.csv

Upvotes: 1

Views: 1346

Answers (1)

Jeff Puckett
Jeff Puckett

Reputation: 40861

in order to avoid the warning, you need to change if(isset($_POST["submit"])) to if(isset($_FILES['file']['tmp_name']))

your form looks OK and the uploads work for me just fine. make sure you have enabled error reporting and that you have uploads enabled in your php.ini

file_uploads = On

Upvotes: 1

Related Questions