RE: Re: [PHP] file_exists and wildcard/regex
| From: | Boyd, Todd M. | Date: | Wed, 10 Dec 2008 14:28:24 +0000 |
| Subject: | RE: Re: [PHP] file_exists and wildcard/regex | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-284417@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Per Jessen [mailto:per@computer.org]
> Sent: Wednesday, December 10, 2008 1:43 AM
> To: php-general@lists.php.net
> Subject: Re: [PHP] file_exists and wildcard/regex
>
> Ashley Sheridan wrote:
>
> > If you're on a Linux system, you could look at ls and the regular
> > expressions it lets you use with it. You could exec out and get the
> > returned results. Also, as it's a system call, it should be very
> > speedy.
>
> 'ls' is just a plain binary (/bin/ls), not a system call. The regex
> functionality is part of the shell, usually bash.
I think the fact that you have to exec() in order to perform it disqualifies it from being a system
call.
Ash - a system call is "a mechanism for software to request a particular kernel service."
There's a somewhat-outdated list from Linux kernel 2.2 at [1]. You might be able to get crafty
with sys_*stat, but I wouldn't recommend it. ;)
FWIW, I would probably do the file search like this (UNTESTED):
<?php
$filereg = '/bfile[1-9]?\d+\.txt/i';
$pathstr = '/whatever/your/path/is';
if(is_dir($pathstr)) {
if($dir = opendir($pathstr)) {
$found = false;
while(($file = readdir($dir)) !== false && !$found) {
if(preg_match($filereg, $file) > 0) {
echo $file . '<br />';
$found = true;
}
}
closedir($dir);
}
}
?>
If you want back more than the first match, do away with the $found -related stuff.
1. http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html
HTH,
// Todd