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());
}
?>
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");
}
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With