Hi, I need to check if a file with a certain string in its filename exists. How would I do that?
file_exists() question.
Collapse
X
-
Tags: None
-
Yeah, I tried glob, but it's very inefficient. So I used a looping readdir();
Here's the function. Works for my circumstances, don't know about anyone else's:
[PHP]/** Checks whether a file with the specified string in its name
* exists in the specified directory.
* Returns 1 if any matching file is found, 0 if not.
*/
function file_exists_con taining ($directory, $string) {
$dirhandle = opendir($direct ory);
$exists = 0;
while($file = readdir($dirhan dle)) {
if (strstr($file, $string)) {
$exists = 1;
break;
}
}
return $exists;
}[/PHP]Comment
Comment