dylandcor Posted April 10, 2009 Share Posted April 10, 2009 I have a code that checks if a particular word document exists. The content gets updated most weeks and the word documents have the form LeaderMMDDYY.doc. Is there any way to extend this script so that if a file doesn't exist, it will create a link to the most recent file going back week by week until it finds one? All I would need it to change would be $last date. I know that this script is laziness at it's finest, but it would make my life easier. $last_date = "032909"; //This is what I need to change with the loop. $sunday = date('mdy', strtotime('last sunday')); if (file_exists("../a/b/Leader$sunday.doc")){ $leader = "<a href='http://www.domain.com/a/b/Leader$sunday.doc'>Leader Questions</a>"; }else{ $leader = "<a href='http://www.domain.com/a/b/Leader$last_date.doc'>Leader Questions</a>"; } Any suggestions would be greatly appreciated. Thanks! Quote Link to comment Share on other sites More sharing options...
thebadbad Posted April 10, 2009 Share Posted April 10, 2009 We can read in all the filenames with glob() and then find the most recent: <?php $files = glob('../a/b/Leader*.doc'); $newest = 0; foreach ($files as $file) { $filename = basename($file); //construct standard date (yyyy-mm-dd) from filename $stddate = '20' . substr($filename, 10, 2) . '-' . substr($filename, 6, 2) . '-' . substr($filename, 8, 2); //if file's date is more recent than the most recent yet, update the most recent $newest if (strtotime($stddate) > $newest) { $newest = strtotime($stddate); } } $last_date = date('mdy', strtotime($newest)); ?> Your code except the first line would then follow. Quote Link to comment Share on other sites More sharing options...
dylandcor Posted April 10, 2009 Author Share Posted April 10, 2009 Thank you for your quick reply. Your code seems to be working except one tiny thing. It links to Leader041000.doc? Any suggestions? This is my current code $files = glob('./a/b/Leader*.doc'); $newest = 0; foreach ($files as $file) { $filename = basename($file); //construct standard date (yyyy-mm-dd) from filename $stddate = '20' . substr($filename, 10, 2) . '-' . substr($filename, 6, 2) . '-' . substr($filename, 8, 2); //if file's date is more recent than the most recent yet, update the most recent $newest if (strtotime($stddate) > $newest) { $newest = strtotime($stddate); } } $last_date = date('mdy', strtotime($newest)); $sunday = date('mdy', strtotime('last sunday')); if (file_exists("./a/b/Leader$sunday.doc")){ $leader = "<a href='http://www.domain.com/a/b/Leader$sunday.doc'>Leader Questions</a>"; }else{ $leader = "<a href='http://www.domain.com/a/b/Leader$last_date.doc'>Leader Questions</a>"; } Quote Link to comment Share on other sites More sharing options...
thebadbad Posted April 10, 2009 Share Posted April 10, 2009 What's the output of echo '<pre>', print_r($files, true), '</pre>'; if you run that after the glob() function? Quote Link to comment Share on other sites More sharing options...
dylandcor Posted April 10, 2009 Author Share Posted April 10, 2009 This is what came up: Array ( [0] => ./documents/agendas/Leader030809.doc [1] => ./documents/agendas/Leader031509.doc [2] => ./documents/agendas/Leader032209.doc [3] => ./documents/agendas/Leader032909.doc [4] => ./documents/agendas/Leader041209.doc ) Quote Link to comment Share on other sites More sharing options...
thebadbad Posted April 10, 2009 Share Posted April 10, 2009 Ah, had a simple mistake. strtotime() shouldn't be run on $newest, since it's already a time stamp: $last_date = date('mdy', $newest); And if you simply want to link to the latest doc, you can shorten your code: <?php $files = glob('./a/b/Leader*.doc'); $newest = 0; foreach ($files as $file) { $filename = basename($file); //construct standard date (yyyy-mm-dd) from filename $stddate = '20' . substr($filename, 10, 2) . '-' . substr($filename, 6, 2) . '-' . substr($filename, 8, 2); //if file's date is more recent than the most recent yet, update the most recent $newest if (strtotime($stddate) > $newest) { $newest = strtotime($stddate); } } $last_date = date('mdy', $newest); $leader = "<a href='http://www.domain.com/a/b/Leader$last_date.doc'>Leader Questions</a>"; ?> Quote Link to comment Share on other sites More sharing options...
dylandcor Posted April 10, 2009 Author Share Posted April 10, 2009 Thank you SO much for sticking with me. I really appreciate it! Problem solved. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted April 10, 2009 Share Posted April 10, 2009 You're welcome! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.