cron??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

cron??

Post by pleigh »

ok here's the idea...i have a directory in my site where i dump my pdf files, lets call the directory myPDFs...inside the directory, i have one.pdf, two.pdf, three.pdf...now, every week, i need to update and replace the content of that directory, having the same file names, overwriting the previous files...these files will be coming from another directory, lets call it my2ndPDFs...i want it to update automatically instead of uploading every week the updated files....i was thinking of cron, but i am not very familiar with it..and don't really know where to start...is there any function that can do this??with or without the cron??i really don't know cron so i may have the wrong impression...please guide me...thanks in advance... :)
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

By personal experience, I say NEVER rely on cron for anything.
I personally always do my "cron jobs" thru php, using time() or date(), so like in your index.php (which I assume would be the page most visited on your site) just make a check if 1 week has passed since your last "updated" and if it has just run the php script to do the things you wanna do.
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post by pleigh »

thanks for the reply...i was also thinking of doing that...but to start with the code is really stucking me...i dont know where to start.. Embarassed ...any functions for this??or class maybe??
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

well personally for all of my sites I have a config table with 2 fields <name> <value> in which I store all the "1 time" information related to the site.
So for your case you could have like:
lastupdate -> time of last update

so lets say you update now and store time().
in your index you can do this:

Code: Select all

$lastupdate = mysql_result(mysql_query("SELECT value FROM config WHERE name='lastupdate'"),0);
then you know that in 1 week heres 604800 seconds, so:

Code: Select all

$timenow = time();
if($lastupdate - $timenow > 604800) //more than 1 week passed
{
        //do your stuff here
        //and update the database
        mysql_query("UDPATE config SET value='$timenow'  WHERE name='lastupdate'");
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I use cron all the time to get stuff done - it works great if you set it up properly. For something like this, I'd make a PHP script that moves all the PDFs from my2ndPDFs to myPDFs

Code: Select all

$from_path = '/var/www/mypdfs';
$to_path = '/var/www/my2ndpdfs';

chdir($from_path);
$files = glob('*.pdf');

foreach($files as $filename)
{
  rename($from_path.'/'.$filename,$to_path.'/'.$filename);
}
Set the permissions so that it's executable, then toss it in your cron.weekly directory. Done.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Flamie wrote:By personal experience, I say NEVER rely on cron for anything.
Hmmm...bit of a flipant remark. Used correctly it the THE best way of scheduling task, when the tasks are time critical.

You alternative method is no use for time critical tasks.
Post Reply