Jump to content

Archived

This topic is now archived and is closed to further replies.

mwq27

php script to upload csv to mysql

Recommended Posts

Hi,

I am stuck in my script.  I have a folder on my server with .csv files in it.  I am writing a php script that will pull all of those files into a mysql database.  My script should locate the directory that holds the .csv files, and break the .csv files up so that the information can be inserted into the Mysql table.  I got it working so that id have to manually enter the file name in the php script to upload it, but I need to be able to upload the entire folder.  Could someone please help me with searching the directory for .csv files and adding them to an array?  thanks

Share this post


Link to post
Share on other sites
[code]<?


mysql_connect($host,$user,$pass) or die("Unable to connect to database");
@mysql_select_db("$database") or die ("Unable to select database $database");
 
  //chdir ("csv/");
  $curdir =  opendir(".");
 
 
  while($files = readdir($curdir))
{ //$split = explode (".", $files);
//$check = $split[0];
$ext = '.csv';
if(strpos($files, $ext) === true){

$filearray[] = $files;


}
$length = count($filearray);

// $filesplit = explode(".", $filename);
//$check_filename = $filesplit[0];

//closedir($dcontents);
  $fcontents = file ($files);
  # expects the csv file to be in the same dir as this script with the name g.csv
for($y=0; $y<=$length; $y++) {

  for($i=1; $i<sizeof($fcontents); $i++) {
      $line = trim($fcontents[$i]);
      $arr = explode(",", $line);
      #if your data is tab separated
      # instead of comma separated,
      # change the ',' above to '\t'
   
      $sql = "insert into $table values ('".
                  implode("','", $arr) ."')";
      mysql_query($sql);
      echo $sql ."<br>\n";
 
      if(mysql_error()) {
        echo mysql_error() ."<br>\n";
      }
  }
  }
 
}echo "<a href=upload4.php>Run script again to upload a new file, but first check the file names.</a>";
?>[/code]

I have been trying to change things aruond a lot and it just wont work.  This file is in a directory called CSV.  The .csv files are also in this directory.

Share this post


Link to post
Share on other sites
Here is my code now.  It keeps telling me that the extension is wrong, when the file in the directory is a .csv file.  Please help

[code]<?php


mysql_connect($host,$user,$pass) or die("Unable to connect to database");
@mysql_select_db("$database") or die ("Unable to select database $database");
  $file_types_array = array(".csv");
    $limitedext = array(".csv"); //Extensions you want files uploaded limited to. also you can use:  //array(".gif",".jpg",".jpeg",".png",".txt",".nfo",".doc",".rtf",".htm",".dmg",".zip",".rar",".gz",".exe");
  //chdir ("csv/");
  $curdir =  ("/home/dvdblurb/public_html/joomla/php/csv/");
  // $dir = opendir($curdir);
 
  if ($handle = opendir($curdir))
 
{
      while (false !== ($filename = readdir($handle))) {
  //echo $filename;
//$split = explode (".", $files);
//$check = $split[0];
// $origfilename = $_FILES["file"]["name"][$key];
      //  $filearray[] = $filename;
 
$filename_array = explode(".", $filename);
$c = count($filename_array);
echo $c;
if($filename_array[$c] !== "csv") { die("error message"); }




//Set valid extension to TRUE




/* $filename = explode(".", $filename);
        $filenameext = $filename[count($filename)-1];
        unset($filename[count($filename)-1]);
        $filename = implode(".", $filename);
        $filename = substr($filename, 0, 15).".".$filenameext;
        $file_ext_allow = FALSE;
for($x=0;$x<=count($file_types_array);$x++){
if($filenameext==$file_types_array[$x])
          {
            $file_ext_allow = TRUE;
         
}
//$ext = '.csv';
}

}*/

//if(strpos($files, $ext) === true){
$filearray[] = $filename;
$length = count($filearray);

// $filesplit = explode(".", $filename);
//$check_filename = $filesplit[0];

//closedir($dcontents);
  $fcontents = file ($filename);
  # expects the csv file to be in the same dir as this script with the name g.csv
for($y=0; $y<=$length; $y++) {

  for($i=1; $i<sizeof($fcontents); $i++) {
      $line = trim($fcontents[$i]);
      $arr = explode(",", $line);
      #if your data is tab separated
      # instead of comma separated,
      # change the ',' above to '\t'
   
      $sql = "insert into $table values ('".
                  implode("','", $arr) ."')";
      mysql_query($sql);
      echo $sql ."<br>\n";
 
      if(mysql_error()) {
        echo mysql_error() ."<br>\n";
      }
  }
  }

}



}echo "<a href=upload4.php>Run script again to upload a new file, but first check the file names.</a>";
?>[/code]

Share this post


Link to post
Share on other sites

×

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.