Jump to content

Question - how to stop fgetcsv() expects parameter 1 to be resource


patsfans

Recommended Posts

I have a script with the code below that inserts a .csv file into a table, and had an issue where if no file is selected, it throws "fgetcsv() expects parameter 1 to be resource, boolean given" about 1,000,000 times or so until it fills up the error log and the hard drive.  I'm guessing someone probably has a simple solution on the below code on how to prevent that going forward, but I've just spent some time looking into it and haven't been able to find the solution.

 

<?php

$conn = mysql_connect("localhost", "dbuser", "dbpassword") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

// Delete existing values in test table before inserting updated file

$sql_ini = "TRUNCATE table";
      mysql_query($sql_ini) or die(mysql_error());

if(isset($_POST['SUBMIT']))
{
$file = $_FILES['file']['tmp_name'];

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

while(($fileop = fgetcsv($handle,1000,",")) != false)
{
	$field1 = $fileop[0];
	$field2 = $fileop[1];
	$field3 = $fileop[2];

	// check for default values and delete locked row before inserting data

	if (!empty($field1))
	{
	// Insert .csv file into database

$sql = "INSERT INTO table (field1, field2, field3) values ('$field1', '$field2', '$field3')";
     mysql_query($sql) or die(mysql_error());
		 }
}
if($sql)
   	{ 
   	// Show whether or not the file was added successfully:
   	
   echo "CSV file successfully imported.";

} else {

	echo "Data Insert Failed";
}
} 

}

?>


<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data">
    <input type="file" name="file" />
    <br>
    <br>
    <input type="submit" name="SUBMIT" value="Submit" />
</form>

 

Thank you in advance for your assistance, and I appreciate your help. :)

 

I have a script with the code below that inserts a .csv file into a table, and had an issue where if no file is selected, it throws "fgetcsv() expects parameter 1 to be resource, boolean given" about 1,000,000 times or so until it fills up the error log and the hard drive.  I'm guessing someone probably has a simple solution on the below code on how to prevent that going forward, but I've just spent some time looking into it and haven't been able to find the solution.

 

<?php

$conn = mysql_connect("localhost", "dbuser", "dbpassword") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

// Delete existing values in test table before inserting updated file

$sql_ini = "TRUNCATE table";
      mysql_query($sql_ini) or die(mysql_error());

if(isset($_POST['SUBMIT']))
{
$file = $_FILES['file']['tmp_name'];

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

while(($fileop = fgetcsv($handle,1000,",")) != false)
{
	$field1 = $fileop[0];
	$field2 = $fileop[1];
	$field3 = $fileop[2];

	// check for default values and delete locked row before inserting data

	if (!empty($field1))
	{
	// Insert .csv file into database

$sql = "INSERT INTO table (field1, field2, field3) values ('$field1', '$field2', '$field3')";
     mysql_query($sql) or die(mysql_error());
		 }
}
if($sql)
   	{ 
   	// Show whether or not the file was added successfully:
   	
   echo "CSV file successfully imported.";

} else {

	echo "Data Insert Failed";
}
} 

}

?>


<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data">
    <input type="file" name="file" />
    <br>
    <br>
    <input type="submit" name="SUBMIT" value="Submit" />
</form>

 

Thank you in advance for your assistance, and I appreciate your help. :)

 

replace

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

 

with

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

 

This will check whether the file can be read or not and allow further script to execute only if $handle is true.

 

Hope this works for you.

This will check whether the file can be read or not and allow further script to execute only if $handle is true.

 

I'm not sure what gave you that impression. The @ symbol simply suppreses errors.

 

The op needs to wrap there while loop in a check that makes sure that $handle is indeed a resource.

 

if ($handle = fopen($file,"r")) {
  // while loop in here
}

This will check whether the file can be read or not and allow further script to execute only if $handle is true.

 

I'm not sure what gave you that impression. The @ symbol simply suppreses errors.

 

The op needs to wrap there while loop in a check that makes sure that $handle is indeed a resource.

 

if ($handle = fopen($file,"r")) {
  // while loop in here
}

 

Cool - I had a feeling that would do it but didn't realize it required an "if" statement to do it.  I'll give that a shot and see if that solves it. :)

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.