Jump to content

FOpen works for some files, not others


tpimental

Recommended Posts

I've got an interesting issue happening.  I get the following error when uploading a CSV file with more than 748 rows:

 

Warning: fgetcsv(): supplied argument is not a valid stream resource in /file/ on line 33

 

Here's the code:

 

$handle = fopen($file, "r");

while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) { //<--this is row 33, but the error is occurring with the fopen

	if ($rowcnt != 1) {
		if ($data[7] == "") {
			$data7 = date("Y-m-d H:i:s",strtotime($inspectDate));
		} else {
			$data7 = date("Y-m-d H:i:s",strtotime($data[7]));
		}

   $query = "INSERT INTO reportdevices
		VALUES(
		$data[0],
		'$data[6]',
		'$data[8]',
		'$data7',
		$_GET[reportID]
		);";

		$errnct = 0;
			if ($data[6] != "") { //skip blank rows (rows without pass/fail)
				$db->query("$query");
			} else {
				$devcnt--;
			}
	}
    $rowcnt++;
    $devcnt++;
  
} 

 

Here's a sample row from a CSV file:

 

16424,1000894,TS,13 fl. Stair 1 closet,476,1,p

 

This is so strange, because I can upload all the CSV files I want as long as there are less than 748 rows.  My first guess was this was a php.ini issue, but a 748 row CSV file is about 30K... go up to 1000 rows and its a whopping 60k... not even close to my upload limits.

 

Any thoughts?  This one is really starting to tick me off.

 

Link to comment
https://forums.phpfreaks.com/topic/111586-fopen-works-for-some-files-not-others/
Share on other sites

Try this...

<?php

$handle = fopen($file, "r");

// assuming you have php5, leave the second parameter null
while (($data = fgetcsv($handle, null, ",")) !== FALSE) {

if ($rowcnt != 1) {

	if ($data[7] == "") {
		$data7 = date("Y-m-d H:i:s",strtotime($inspectDate));
	} else {
		$data7 = date("Y-m-d H:i:s",strtotime($data[7]));
	}

	$query = sprintf("INSERT INTO reportdevices VALUES( %d, '%s', '%s', '%s', %d)",
			$data[0],
			$data[6],
			$data[8],
			$data[7],
			$_GET['reportID']);

	$errnct = 0;

	if ($data[6] != "") {
		$db->query($query);
	} else {
		$devcnt--;
	}
}

$rowcnt++;
$devcnt++;
  
} 

 

I'm not familiar with the fgetcsv command, so my inclination is that the problem is there.  You may want to try reading the file into an array (http://www.php.net/file) and looping through it that way, then exploding each line on the commas.

Thanks for the tip hitman, but I get the same error with your code.  I'm 99% sure the problem is with the fopen call, because if I add an IF statement around the fopen, it stops there.  For example:

 

<?php
if(!$handle = fopen($file, "r"))
{
     echo 'Error: could not open file';
      die;
}
?>

 

The error prints out and the script dies.  But I'm completely open to suggestions.

 

Thanks!

When I added the code, nothing comes up which I assume means the file isn't being loaded.  I'm still stuck, because files with less than 748 rows does spit out results, but more than that doesn't.

 

 

Are you sure that $file exists and is readable by the Apache (or IIS, depending) user can access it?  Use stat...

 

echo '<pre>' . print_r(stat($file), true);

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.