Jump to content


Photo

php script to upload csv to mysql


  • Please log in to reply
3 replies to this topic

#1 mwq27

mwq27
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 16 October 2006 - 11:29 PM

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

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 16 October 2006 - 11:51 PM

Post your code.

#3 mwq27

mwq27
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 16 October 2006 - 11:56 PM

<?


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>";
?>

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.

#4 mwq27

mwq27
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 17 October 2006 - 03:10 AM

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

<?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>";
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users