Jump to content

Help with CSV import.


kscrow

Recommended Posts

I have been having quite a time with this. I have the following code which I am attempting to use to upload a csv file then read the contents and import the rows into mysql. I am getting my own error message back "Invalid file". Strangely, if I echo the $fname right after the variable is declared, I get nothing. Same is true if I try to echo the $chk_ext[0] or $chk_ext[1]. My eyes are all worn out and I'm ready to hurt myself. Can anyone offer any guidance on this. I am new to uploading nad importing files so any help would be appreciated.  Thanks!  K

 

if(isset($_POST['submit']))

{

    $fname = $_FILES['sel_file']['name'];

   

    $chk_ext = explode(".",$fname);

   

    if(strtolower($chk_ext[1]) == "csv")

    {

   

        $filename = $_FILES['sel_file']['tmp_name'];

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

   

        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)

        {

            $sql = "INSERT into runs(number,date,ordering,caller,dept,destination) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]')";

            mysql_query($sql) or die(mysql_error());

        }

   

        fclose($handle);

        echo "Successfully Imported";

    }

    else

    {

        echo "Invalid File";

    }   

}

?>

<form action='<?php echo $_SERVER["PHP_SELF"];?>' method='post'>

 

    Import File : <input type='file' name='sel_file' size='20'>

    <input type='submit' name='submit' value='submit'>

 

</form>

Link to comment
https://forums.phpfreaks.com/topic/195269-help-with-csv-import/
Share on other sites

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.