bschultz Posted April 15, 2011 Share Posted April 15, 2011 How do I write a bit of code to check if a file is NOT readable...and if (and only if) it is NOT readable (doesn't exist) , wget the file. Here's what I tried...but it loops forever and ever...even if the file does exist. $filename = "file.mp3"; while(!is_readable($filename)) { $url = "domain.com" . ".mp3"; echo system('wget '.escapeshellcmd($url)); echo system('sleep 30'); } ?> I Googled...and couldn't find the correct syntax for IS NOT READABLE. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/233820-while-file-is-not-readable-problem/ Share on other sites More sharing options...
drisate Posted April 15, 2011 Share Posted April 15, 2011 You should use file_exists() instead Why do you need to use while? Theres only one to check. if (file_exists($filename)){ $url = "domain.com" . ".mp3"; echo system('wget '.escapeshellcmd($url)); echo system('sleep 30'); } If you have more then one then you can do something like <?php // Build an array of your files $filename[] = "file1.mp3"; $filename[] = "file2.mp3"; $filename[] = "file3.mp3"; $filename[] = "file4.mp3"; $filename[] = "file5.mp3"; foreach($filename as $file => $value){ if (file_exists($value)){ //[..] continue with your code using $value for the file name } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/233820-while-file-is-not-readable-problem/#findComment-1202046 Share on other sites More sharing options...
bschultz Posted April 15, 2011 Author Share Posted April 15, 2011 Since wget fails (and stops) on a 404 error, I want to run a cron job to see if the file exists locally...and if not, try to download it. So, while the file does not exist (or is not readable)...continue trying to wget it. My code above was wrong...where it says domain.com, should be the local file system. Quote Link to comment https://forums.phpfreaks.com/topic/233820-while-file-is-not-readable-problem/#findComment-1202052 Share on other sites More sharing options...
bschultz Posted April 15, 2011 Author Share Posted April 15, 2011 I've figured this out via a shell script to execute the php download script if the file doesn't exist...but I don't like running two scripts when one should work. Oh well...thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/233820-while-file-is-not-readable-problem/#findComment-1202076 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.