问题
Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
Hi i'm trying to process a csv with fgetcsv but i all i'm going is an infinite loop of errors
Warning: fopen("Tile","User"...) in testcsv.php on line 5
Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /home/ratena/public_html/proc_files/testcsv.php on line 6
<?php
$uploadcsv = "/home/ratena/public_html/temp/files/BatchLoadPM15.csv";
$filecsv = file_get_contents($uploadcsv);
//$batchid = $_POST['batchid'];
$handle = fopen("$filecsv", "r");
while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) {
print_r($data);
}
?>
回答1:
this part having problem
/* this is un-necessary */
$filecsv = file_get_contents($uploadcsv);
/* this expecting a file in */
$handle = fopen("$filecsv", "r");
Try replace the 3 lines with just this
$handle = fopen($uploadcsv, 'r');
To skip first row
$column_headers = array();
$row_count = 0;
while (($data = fgetcsv($handle, 100000, ",")) !== FALSE)
{
if ($row_count==0)
{
$column_headers = $data;
}
else
{
print_r($data);
}
++$row_count;
}
回答2:
You are downloading the CSV file into a string $filecsv and using this string as filename in fopen!!
There is no need to download the file, just pass the name of the CSV file to fopen.
Also remember to check the return value of fopen always before you go ahead and read.
$uploadcsv = "/home/namebob/public_html/temp/files/BatchLoadPM15.csv";
// try to open the file.
$handle = fopen($uploadcsv, "r");
// error checking.
if($handle === false) {
die("Error opening $uploadcsv");
}
// to skip the header.
$skip = true;
// file successfully open..now read from it.
while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) {
// if header..don't print and negate the flag.
if($skip) {
$skip = !$skip;
// else..print.
} else {
print_r($data);
}
}
回答3:
It's because you're trying to open the contents of a file, rather than the file name. Remove the file_get_contents line, and replace the fopen call with:
$handle = fopen($uploadcsv, 'r');
That should do it...
回答4:
Looks like your first row contains headers. Skip row one.
来源:https://stackoverflow.com/questions/4327833/php-return-error-when-using-fgetcsv