This is a discussion on scheduling a script to check a directory for files within the PHP General forums, part of the PHP Programming Forums category; Greetings! I need to write a script to import '.csv' data files into MySQL. My question is how can I ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Greetings!
I need to write a script to import '.csv' data files into MySQL. My question is how can I have a script execute and check a directory every 4 hours for any '.csv' files and if it finds any calls a function to import them? Thanks, Dave ************************************************** ******************** HTC Disclaimer: The information contained in this message may be privileged and confidential and protected from disclosure. If the reader of this message is not the intended recipient, or an employee or agent responsible fordelivering this message to the intended recipient, you are hereby notifiedthat any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify us immediately by replying to the message and deleting it from your computer. Thank you. ************************************************** ******************** |
|
|||
|
On a *nix-box it's fairly simple when using cronjobs... maybe try that?
----- Original Message ----- From: "Bosky, Dave" <Dave.Bosky@htcinc.net> To: <php-general@lists.php.net> Sent: Wednesday, May 30, 2007 2:14 PM Subject: [php] scheduling a script to check a directory for files Greetings! I need to write a script to import '.csv' data files into MySQL. My question is how can I have a script execute and check a directory every 4 hours for any '.csv' files and if it finds any calls a function to import them? Thanks, Dave ************************************************** ******************** HTC Disclaimer: The information contained in this message may be privileged and confidential and protected from disclosure. If the reader of this message is not the intended recipient, or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify us immediately by replying to the message and deleting it from your computer. Thank you. ************************************************** ******************** |
|
|||
|
> I need to write a script to import '.csv' data files into MySQL.
> > My question is how can I have a script execute and check a directory > every 4 hours for any '.csv' files and if it finds any calls a function > to import them? On linux there is cron (you should find many example by googling) on windows there is the task scheduler... And then finally you can use PHPs delay func and a loop ;) The any part can be done by globbing, DirectoryIterator, shell scripts ... Marc |
|
|||
|
On Wed, May 30, 2007 7:14 am, Bosky, Dave wrote:
> I need to write a script to import '.csv' data files into MySQL. > > My question is how can I have a script execute and check a directory > every 4 hours for any '.csv' files and if it finds any calls a > function > to import them? Linux: man 5 crontab * */4 * * * /usr/local/bin/php -q import.php Windows: Scheduled Tasks [interminible dialogs here] -------- import.php -------- <?php //Untested code //Needs better error handling //Will not scale well to *large* .csv files... //Use mysql LOAD DATA IN FILE for large csv files $path = '/path/to/directory'; $dir = opendir($path) or die("Failed to open '$path'"); while (($file = readdir($dir)) !== false){ if ($file == '.' || $file == '..') continue; if (!preg_match('|.csv$|i', $file)){ echo "What's '$file' doing in the .csv import directory?\n"; continue; } $f = fopen("$path/$file", 'r'); if (!$f){ echo "Failed to open '$path/$file'\n"; continue; } while (!feof($f)){ $row = fgetcsv($f, 1000000); array_map('mysql_real_escape_string', $row); $row_sql = implode("', '", $row); $query = "insert into whatever values ('$row_sql')"; mysql_query($query) or die(mysql_error()); } } ?> -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? |
![]() |
| Thread Tools | |
| Display Modes | |
|
|