Page 1 of 1

Import data from csv file to mysql

Posted: Wed Oct 07, 2009 4:27 am
by phphunger
Hi techies,

As i am working on file upload concept in php. When i browse the csv file from any directory to upload data to mysql i am getting the following error. Apart from getting errors the empty records are added to the mysql database and also the script will continue executing.(which goes to the infinite loop)

The errors as follows:

Code: Select all

Warning: fopen() [function.fopen]: Filename cannot be empty in C:\xampp\htdocs\upload\import1.php on line 11
 
Warning: fgetcsv() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\upload\import1.php on line 13
 
 
Code:

Code: Select all

 
<?php
include "connect.php";
 
if(isset($_POST['submit']))
 
   {
 
     $filename=$_FILES['filename'];
 
     $handle = fopen("$filename", "r");
 
     while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
 
     {
 
       $import="INSERT into info(name,address,food) values('$data[0]','$data[1]','$data[2]')";
 
       mysql_query($import) or die(mysql_error());
 
     }
 
     fclose($handle);
 
     print "Import done";
 
   }
 
   else
 
   {
 
       print "<form action='import1.php' method='post'>";
 
          print "Browse file to import:</br>";
           
      print "<input type='file' name='filename'></br>";
 
      print "<input type='submit' name='submit' value='submit'></form>";
 
   }
 
 
?>
Cheers....

phphunger

Re: Import data from csv file to mysql

Posted: Wed Oct 07, 2009 5:03 am
by dejvos
A global $_FILES contains an array of sended files. So it has name of your "input" on firstlevel in case of sending datas throught html form. I have experience with some java uploader and it uses a bit different ordering.

Important is that you have to identified a name of your file input.

Code: Select all

<?php
// For html form
$_FILES[<inputName>] = array('filename' => ,'tmp_name' => ....);
 
//For Java uplaoder
$_FILES['filename'][<inputName>] = '';
$_FILES['tmp_name'][<inputName>] = '';
?>
Bye.

Re: Import data from csv file to mysql

Posted: Wed Oct 07, 2009 5:26 am
by phphunger
i didnt understand your view..can you explain clearly.

Re: Import data from csv file to mysql

Posted: Wed Oct 07, 2009 7:12 am
by dejvos
I guess the formular bellow:

Code: Select all

 
<form method="POST">
<input type="file" name="uploadedFile" />
<input type="submit" name="submit" value="Send"/>
</form>
 
will send $_FILES in this form:

Code: Select all

<?php
$_FILES['uploadedFile'] = array('filename' => 'file.txt','tmpname' => '/tmp/xadhikjk', ...);
?>
The first index is missing in your code. I think your line 9 should look like:

Code: Select all

 
$filename=$_FILES['uploadedFile']['filename'];
 
And one more think: The file is stored in $_FILES['uploadedFile']['tmp_name'].

Re: Import data from csv file to mysql

Posted: Wed Oct 07, 2009 7:33 am
by phphunger
I want to upload the csv file which is not in a particular folder but the csv file will be anywhere in the system. I want to browse that file and upload it to the mysql database.. Further clarifaction required..

Re: Import data from csv file to mysql

Posted: Wed Oct 07, 2009 8:01 am
by dejvos
I hope I understand. It's possible that I can't write my suggestion properly. So I will try it again, sorry if it will be to particular.

PHP is a server side language, so you need to move the csv file from your machine to a server. It's possible throught "<input type="file" name="filename' />" as you have in your html. This tag will cause this:
  • The file will be copied from your machine to a server's temp.
  • Your target script will receive the global variable $_FILES. The variable has,in your case, this form:

    Code: Select all

    $_FILES['filename']['tmp_name'] = '/tmp/somename'; //path to the file on server - the only possible way how to reach the file
    $_FILES['filename']['name'] = 'file_name'; //the name of file -> string; this is not a path!
    ... and some other values http://us3.php.net/manual/en/reserved.variables.files.php
So If you want operate with your csv you have to use 'tmp_name'. In your script will be in the var $filename an array.

Is it clear right now?

Bye

Re: Import data from csv file to mysql

Posted: Thu Oct 08, 2009 2:01 am
by phphunger
Thanks dejvos. I got it...Great support...