Jump to content

check files exist


Go to solution Solved by Jacques1,

Recommended Posts

Hi,

 

I have an array of files, and i want to check they ALL exist.

 

I have put together the following, but it doesnt check they all exist correctly -  it simply returns true, false for each individual file so is a bit of a lottery depending on the order of whats going to happen. Is there an easy way im overlooking to do this? should I be setting a variable and then checking that out-with the loop?

 

Thanks,

 

MoFish

$filenames = array("index.html", "areas.html", "test.html", "example.xls");
foreach ($filenames as $file){
    if(!file_exists($this->skeleton_dir.$file)){
        return false;
    }else{
        return true;
    }
}
Link to comment
https://forums.phpfreaks.com/topic/300923-check-files-exist/
Share on other sites

  • Solution

You have to move the return true behind the loop that that it's only executed if all files exist.

 

Or use a boolean variable for clarity:

$filenames = ["index.html", "areas.html", "test.html", "example.xls"];

$all_files_exist = true;
foreach ($filenames as $filename)
{
    if(!file_exists($this->skeleton_dir.$filename))
    {
        $all_files_exist = false;
        break;
    }
}

return $all_files_exist;
Edited by Jacques1
  • Like 1
Link to comment
https://forums.phpfreaks.com/topic/300923-check-files-exist/#findComment-1531671
Share on other sites

I don't see why the function should be artificially restricted to regular files. Checking the existence of folders as well is a very natural use case, and it would be rather confusing for the function to fail in that case.

 

If, for whatever strange reason, there may be empty filenames, I'd check exactly that.

Link to comment
https://forums.phpfreaks.com/topic/300923-check-files-exist/#findComment-1531679
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.