Checking a single file using the code below works.. but I have to check many files in a loop. I am working on a project that have a lots of files (around 1000). File name differs just on prefix with some breaks. It is a project using CI framework.
Case I: using is_file() and file_exists()
<?php function custom_file_exists($filePath)
{
if((is_file($filePath))&&(file_exists($filePath))){
return 'true';
}
return 'false';
}
?>
Case II: using glob()
<?php function custom_file_exists($filePath) {
$files = glob('uploads/folder/*.pdf', GLOB_BRACE);
if (in_array($filePath, $files)) {
return 'true';
} else {
return 'false';
}
}
?>
Here is the part of the code that I want to work on. File check function works when passing a single filePath but fails when I do the same using following loop.
<?php foreach($files as $file):
//checking all files in loop
$search = 'uploads/folder/'.$file->name;
$result = custom_file_exists($search);
echo $result; //return 'false' even if the file is present
//checking for single file
$search1 = 'uploads/folder/filename';
$result1 = custom_file_exists($search1);
echo $result; //return 'true' when file is present
endforeach;?>
I am not being able to figure out what is wrong with the code. Can someone help me out in this ?? Any ideas are appreciated. Thanks for the help.
Var dump of glob function as requested.
<?php $allfiles = glob("uploads/folder/*.pdf");
print_r($allfiles);?>
The result is:
Array ( [0] => uploads/folder/file1.pdf [1] => uploads/folder/file2.pdf [2] => uploads/folder/file3.pdf [3] => uploads/folder/file4.pdf [4] => uploads/folder/file5.pdf [5] => uploads/folder/file6.pdf [6] => uploads/folder/file7.pdf [7] => uploads/folder/file8.pdf ......
For the $files that I am using in loop: print_r($files) gives the following output.
Array ( [0] => stdClass Object ( [id] => 1 [name] => file1.pdf [description] => something here [published_date] => 2016 [publish] => 1 )
[1] => stdClass Object ( [id] => 2 [name] => file2.pdf [description] => something here [published_date] => 2016 [publish] => 1 )
[2] => stdClass Object ( [id] => 3 [name] => file3.pdf [description] => something here [published_date] => 2016 [publish] => 1 )
[3] => stdClass Object ( [id] => 4 [name] => file4.pdf [description] => something here [published_date] => 2016 [publish] => 1 ) ......