Here we will FTP to get file list from FTP, Download them then parse them and insert into MySQL Database.
This is meant to be a behind the scenes script so no output to screen on error. It only exits and if you want to… email errors to admin. Email script in seperate entry.
$dir = "dev/"; //root $server = ftp_connect("ftp.domain.com"); $login = ftp_login($server, "username", "password"); if((!$server) || (!$login)){ //mail error report could not connect to FTP exit; } // get ftp file list $get_ftp_list= ftp_rawlist($server, $dir); array_slice($get_ftp_list, 2); // first 2 entries are useless for us here so cut em! they are "." and ".." // get local file list $local_dir = "dev/"; if ($dh = opendir($local_dir)) { while (($filelist = readdir($dh)) !== false) { $local_files[].= $filelist; // put all files into an array } closedir($dh); } array_slice($local_files, 2); // first 2 entries are useless for us here so cut em! they are "." and ".." //download the files for ($i=0; $i < count($get_ftp_list); $i++){ $handle2 = fopen($get_local_list[$i], "w"); ftp_fget($server, $handle2, $get_ftp_list[$i], FTP_ASCII, 0); fclose($handl2); } ftp_close($server); // close FTP ################################################## // if you want to test arrays before insert comment out mysql_query. // Also make sure MySQL tables are created and file names are the same as table names and columns match row one in file // SQL dump first row must be column names // open downloaded local files and format into SQL statements for insertion $handle = opendir($dir); while (false !== ($list = readdir($handle))) { $files[].= $list; } //print_r($files); // see what we have to chop off the front of the array. usually just 2 $files = array_slice($files, 2); // for each file for($x=0; $x < count($files); $x++){ $file = file($path.$files[$x]); ################################################## //Get aTable name from File name $table_name = explode(".", $files[$x]); // seperate by extention $table = $table_name[0]; // Place File name into string, loose extention ################################################## ################################################## // get column names $columns = explode("\t", $file[0]); // TAB Delimited files //print_r($columns); $columns = implode(", ", $columns); // format string for Columns $columns= str_replace('"', "", $columns); // Remove Quotes Around Column Names. ################################################## ################################################## // Get Values for Insert array_shift($file); // remove header row from array //print_r($file); // Run the inserts for how many rows are in the File for($i=0; $i < count($file); $i++){ // First put values into an array delimited by Tab // Second wrap line with ' // my version of addslashes since mysql_real_escape_string() doesn't work on arrays $bad = array("'", '"'); $good = array("\'", ''); // i have TAB delimited file your's may be , or : or ; $values = explode("\t", "'".str_replace($bad, $good, $file[$i])."'"); // wrap line in single quotes // now add ' in array to synch with above ' this fills in between records $values = implode("', '", $values); // Create sql statement $sql = "INSERT INTO ".$table." (".$columns.") VALUES (".$values.")"; //echo $sql."br -tag"; // MT will not allow me to use the HTML for BR replace br-tag with HTML of BR // Insert mysql_query($sql) or die('Could not Insert values: '.mysql_error()); } // for each insert close ################################################## } // for each $files close
If there is an error let me know it is untested as of yet and the original script checks time logs from FTP and local and compares them to make sure there is no difference in updates