patsfans Posted June 11, 2012 Share Posted June 11, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/264023-question-how-to-stop-fgetcsv-expects-parameter-1-to-be-resource/ Share on other sites More sharing options...
vikrantmohite Posted June 12, 2012 Share Posted June 12, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/264023-question-how-to-stop-fgetcsv-expects-parameter-1-to-be-resource/#findComment-1353079 Share on other sites More sharing options...
trq Posted June 12, 2012 Share Posted June 12, 2012 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 } Quote Link to comment https://forums.phpfreaks.com/topic/264023-question-how-to-stop-fgetcsv-expects-parameter-1-to-be-resource/#findComment-1353112 Share on other sites More sharing options...
patsfans Posted June 12, 2012 Author Share Posted June 12, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/264023-question-how-to-stop-fgetcsv-expects-parameter-1-to-be-resource/#findComment-1353157 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.