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. :)

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.