Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: fgetcsv() expects parameter 1 to be resource, boolean given in C:

Tags:

php

csv

I'm trying to get a CSV with fgetcsv(), but I get an error like this:

Warning: fgetcsv() expects parameter 1 to be resource, boolean given in C:..

If I var_dump($handle) for make sure then the output is : Bool(False)

<?php

    $handle = fopen($_FILES['filename']['tmp_name'], "r");
    //var_dump($handle);die;
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        //print_r ($data);die;
        $import="INSERT into pickup_28(NO,TGL_PICKUP,SERVICE_NUMBER,ICCID,CUSTOMER_NAME,PRODUCT,ALAMAT,NO_ALT,AREA) 
        values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]')";

        mysql_query($import) or die(mysql_error());

    }

?>
like image 468
Delinda Dyta Avatar asked Aug 12 '15 08:08

Delinda Dyta


2 Answers

You need to test whether fopen succeeded before you try to use the result:

$handle = fopen($_FILES['filename']['tmp_name'], "r");
if ($handle) {
    // Use $handle
} else {
    die("Unable to open file");
}
like image 162
Barmar Avatar answered Oct 21 '22 09:10

Barmar


It means that fopen() function returned FALSE;

So you need modify your fopen() function also before using fgetcsv() check if it is resoruce with is_resource() function. You can also check before opening if file is readable with function is_readable() or if exists with file_exists()

like image 2
Robert Avatar answered Oct 21 '22 11:10

Robert