scheduling a script to check a directory for files

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-30-2007
Bosky, Dave
 
Posts: n/a
Default 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 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.
************************************************** ********************


Reply With Quote
  #2 (permalink)  
Old 05-30-2007
Auto-Deppe, C. Haensel
 
Posts: n/a
Default Re: [PHP] scheduling a script to check a directory for files

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.
************************************************** ********************
Reply With Quote
  #3 (permalink)  
Old 05-30-2007
Marc Weber
 
Posts: n/a
Default Re: [PHP] scheduling a script to check a directory for files

> 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
Reply With Quote
  #4 (permalink)  
Old 05-30-2007
Richard Lynch
 
Posts: n/a
Default Re: [PHP] scheduling a script to check a directory for files

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?
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:59 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0