PHP return error when using fgetcsv

Possible duplicate:
mysql_fetch_array () expects parameter 1 to be a resource, boolean is set to select

Hi, I am trying to handle csv using fgetcsv, but I'm all I am going to have is an infinite loop of errors

Warning: fopen ("Tile", "User" ...) in testcsv.php on line 5

Warning: fgetcsv () expects parameter 1 to be a resource, boolean is set 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);   
        }
?>
+3
source share
4 answers

this part has a problem

/* this is un-necessary */
$filecsv = file_get_contents($uploadcsv);
/* this expecting a file in */
$handle = fopen("$filecsv", "r");

Try replacing 3 lines with this.

$handle = fopen($uploadcsv, 'r');

Skip the first line

$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;
}
+5
source

CSV $filecsv fopen!!

, CSV fopen.

fopen , .

$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);               
    }
}  
+4

, , . file_get_contents fopen :

$handle = fopen($uploadcsv, 'r');

...

0

, . .

-2

Source: https://habr.com/ru/post/1777973/


All Articles