MoFish Posted March 3, 2016 Share Posted March 3, 2016 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/300923-check-files-exist/ Share on other sites More sharing options...
Solution Jacques1 Posted March 3, 2016 Solution Share Posted March 3, 2016 (edited) 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 March 3, 2016 by Jacques1 1 Quote Link to comment https://forums.phpfreaks.com/topic/300923-check-files-exist/#findComment-1531671 Share on other sites More sharing options...
QuickOldCar Posted March 4, 2016 Share Posted March 4, 2016 If were to get an empty value for the filename in the array it would still be true because file_exists() would detect the directory, if is just files use is_file() Quote Link to comment https://forums.phpfreaks.com/topic/300923-check-files-exist/#findComment-1531677 Share on other sites More sharing options...
Jacques1 Posted March 4, 2016 Share Posted March 4, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/300923-check-files-exist/#findComment-1531679 Share on other sites More sharing options...
mac_gyver Posted March 4, 2016 Share Posted March 4, 2016 $filenames = array("index.html", "areas.html", "test.html", "example.xls"); $folder = $this->skeleton_dir; $files = glob("$folder{".implode(',',$filenames)."}",GLOB_BRACE); return (count($filenames) == count($files)); Quote Link to comment https://forums.phpfreaks.com/topic/300923-check-files-exist/#findComment-1531680 Share on other sites More sharing options...
Jacques1 Posted March 4, 2016 Share Posted March 4, 2016 This isn't the Obfuscated Perl Contest. Quote Link to comment https://forums.phpfreaks.com/topic/300923-check-files-exist/#findComment-1531681 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.