Go Back   Forum Care Forums > Development Reference Area > Php Development

Reply
 
LinkBack Thread Tools Display Modes
scheduling a script to check a directory for files
Old
  (#1)
Bosky, Dave
Guest
 
Posts: n/a
Default scheduling a script to check a directory for files - 06-02-2007, 08:56 PM

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


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: [PHP] scheduling a script to check a directory for files
Old
  (#2)
Auto-Deppe, C. Haensel
Guest
 
Posts: n/a
Default Re: [PHP] scheduling a script to check a directory for files - 06-02-2007, 08:56 PM

On a *nix-box it's fairly simple when using cronjobs... maybe try that?

----- Original Message -----
From: "Bosky, Dave" <EMAIL REMOVED>
To: <php-EMAIL REMOVED>
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.
************************************************** ********************
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] scheduling a script to check a directory for files
Old
  (#3)
Marc Weber
Guest
 
Posts: n/a
Default Re: [PHP] scheduling a script to check a directory for files - 06-02-2007, 08:56 PM

> 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
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] scheduling a script to check a directory for files
Old
  (#4)
Richard Lynch
Guest
 
Posts: n/a
Default Re: [PHP] scheduling a script to check a directory for files - 06-02-2007, 08:56 PM

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?
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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

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





Contact Us - Forum Care Forums - Archive - Top