Jump to content

While file is NOT readable problem


bschultz

Recommended Posts

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!

Link to comment
Share on other sites

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

?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.