Discussion:
file_exists and wildcard/regex
Daniel Kolbo
2008-12-09 22:26:12 UTC
Permalink
What is the preferred method with php to test and see if a file
[pattern] exists?

For example, i only need to search in one directory, that may have any
number of files named such as afile1.txt, afile2.txt, afile3.txt, ....
And also, bfile1.txt, bfile2.txt, bfile3.txt, ...
I want to see if any such file 'family' exists. That is, i want to see
if there is any file named bfile[1-9][0-9]+.txt. I don't care which
bfile number exists, i just want to know if any bfile exists.

I hope this is clear enough, if not let me know.

thanks,
dK
Stut
2008-12-09 22:48:30 UTC
Permalink
On 9 Dec 2008, at 22:26, Daniel Kolbo wrote:
> What is the preferred method with php to test and see if a file
> [pattern] exists?
>
> For example, i only need to search in one directory, that may have
> any number of files named such as afile1.txt, afile2.txt,
> afile3.txt, .... And also, bfile1.txt, bfile2.txt, bfile3.txt, ...
> I want to see if any such file 'family' exists. That is, i want to
> see if there is any file named bfile[1-9][0-9]+.txt. I don't care
> which bfile number exists, i just want to know if any bfile exists.
>
> I hope this is clear enough, if not let me know.

Use glob (http://php.net/glob) and get the size of the array returned.
Note that if there could be thousands of matching files you may want
to use opendir and readdir to look for matches instead.

-Stut

--
http://stut.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Daniel Kolbo
2008-12-09 22:54:38 UTC
Permalink
Daniel Kolbo wrote:
> What is the preferred method with php to test and see if a file
> [pattern] exists?
>
> For example, i only need to search in one directory, that may have any
> number of files named such as afile1.txt, afile2.txt, afile3.txt,
> .... And also, bfile1.txt, bfile2.txt, bfile3.txt, ...
> I want to see if any such file 'family' exists. That is, i want to
> see if there is any file named bfile[1-9][0-9]+.txt. I don't care
> which bfile number exists, i just want to know if any bfile exists.
>
> I hope this is clear enough, if not let me know.
>
> thanks,
> dK
>
After some more research it seems my options are:
1) loop through the directory contents
2) use scandir (then search the resulting array)
3) use glob. I am not familiar with the glob pattern library, it does
not seem like i have the full power of regex when using glob is this
correct? Also, searching the whole filesystem seems...overkill.

What do you suggest, is there a 4th option?
thanks,
dK
Ashley Sheridan
2008-12-09 23:05:11 UTC
Permalink
On Tue, 2008-12-09 at 12:54 -1000, Daniel Kolbo wrote:
>
> Daniel Kolbo wrote:
> > What is the preferred method with php to test and see if a file
> > [pattern] exists?
> >
> > For example, i only need to search in one directory, that may have any
> > number of files named such as afile1.txt, afile2.txt, afile3.txt,
> > .... And also, bfile1.txt, bfile2.txt, bfile3.txt, ...
> > I want to see if any such file 'family' exists. That is, i want to
> > see if there is any file named bfile[1-9][0-9]+.txt. I don't care
> > which bfile number exists, i just want to know if any bfile exists.
> >
> > I hope this is clear enough, if not let me know.
> >
> > thanks,
> > dK
> >
> After some more research it seems my options are:
> 1) loop through the directory contents
> 2) use scandir (then search the resulting array)
> 3) use glob. I am not familiar with the glob pattern library, it does
> not seem like i have the full power of regex when using glob is this
> correct? Also, searching the whole filesystem seems...overkill.
>
> What do you suggest, is there a 4th option?
> thanks,
> dK
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.


Ash
www.ashleysheridan.co.uk


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Per Jessen
2008-12-10 07:42:48 UTC
Permalink
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.


/Per Jessen, Zürich


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Boyd, Todd M.
2008-12-10 14:28:24 UTC
Permalink
> -----Original Message-----
> From: Per Jessen [mailto:***@computer.org]
> Sent: Wednesday, December 10, 2008 1:43 AM
> To: php-***@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,
Boyd, Todd M.
2008-12-10 14:35:01 UTC
Permalink
> -----Original Message-----
> From: Boyd, Todd M. [mailto:***@ccis.edu]
> Sent: Wednesday, December 10, 2008 8:28 AM
> To: php-***@lists.php.net
> Subject: RE: Re: [PHP] file_exists and wildcard/regex
>
> FWIW, I would probably do the file search like this (UNTESTED):
>
> <?php
>
> $filereg = '/bfile[1-9]?\d+\.txt/i';

Erp. I meant '/bfile[1-9]?\d\.txt/i', with no '+' after the '\d'... assuming you want 0-99 to be the range of values.

> $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);
> }
>
Maciek Sokolewicz
2008-12-09 23:00:45 UTC
Permalink
Daniel Kolbo wrote:
> What is the preferred method with php to test and see if a file
> [pattern] exists?
>
> For example, i only need to search in one directory, that may have any
> number of files named such as afile1.txt, afile2.txt, afile3.txt, ....
> And also, bfile1.txt, bfile2.txt, bfile3.txt, ...
> I want to see if any such file 'family' exists. That is, i want to see
> if there is any file named bfile[1-9][0-9]+.txt. I don't care which
> bfile number exists, i just want to know if any bfile exists.
>
> I hope this is clear enough, if not let me know.
>
> thanks,
> dK
>

glob()

http://www.php.net/glob

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Daniel Kolbo
2008-12-09 23:24:34 UTC
Permalink
Maciek Sokolewicz wrote:
> Daniel Kolbo wrote:
>> What is the preferred method with php to test and see if a file
>> [pattern] exists?
>>
>> For example, i only need to search in one directory, that may have
>> any number of files named such as afile1.txt, afile2.txt, afile3.txt,
>> .... And also, bfile1.txt, bfile2.txt, bfile3.txt, ...
>> I want to see if any such file 'family' exists. That is, i want to
>> see if there is any file named bfile[1-9][0-9]+.txt. I don't care
>> which bfile number exists, i just want to know if any bfile exists.
>>
>> I hope this is clear enough, if not let me know.
>>
>> thanks,
>> dK
>>
>
> glob()
>
> http://www.php.net/glob
How portable is glob?
How fast is glob? Being that it searches through the entire filesystem,
this could potentially take a long time (like if i have wildcards early
in the filepath pattern and lots of matches) correct? If my file
variations (wildcards) are just at the end of of the filepaths and i
don't have more than 1000 files in the directory then will I most likely
be 'alright' with glob (in terms of time)? I have probably spent more
time now 'considering' the time implications of glob, than glob actually
would consume when operating...

Thanks for the quick response/solutions.
dK
Chris
2008-12-10 00:02:46 UTC
Permalink
Daniel Kolbo wrote:
> Maciek Sokolewicz wrote:
>> Daniel Kolbo wrote:
>>> What is the preferred method with php to test and see if a file
>>> [pattern] exists?
>>>
>>> For example, i only need to search in one directory, that may have
>>> any number of files named such as afile1.txt, afile2.txt, afile3.txt,
>>> .... And also, bfile1.txt, bfile2.txt, bfile3.txt, ...
>>> I want to see if any such file 'family' exists. That is, i want to
>>> see if there is any file named bfile[1-9][0-9]+.txt. I don't care
>>> which bfile number exists, i just want to know if any bfile exists.
>>>
>>> I hope this is clear enough, if not let me know.
>>>
>>> thanks,
>>> dK
>>>
>>
>> glob()
>>
>> http://www.php.net/glob
> How portable is glob?
> How fast is glob? Being that it searches through the entire filesystem,
> this could potentially take a long time (like if i have wildcards early
> in the filepath pattern and lots of matches) correct? If my file
> variations (wildcards) are just at the end of of the filepaths and i
> don't have more than 1000 files in the directory then will I most likely
> be 'alright' with glob (in terms of time)? I have probably spent more
> time now 'considering' the time implications of glob, than glob actually
> would consume when operating...

glob('/path/to/folder/*.txt');

--
Postgresql & php tutorials
http://www.designmagick.com/


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Stut
2008-12-10 00:13:07 UTC
Permalink
On 9 Dec 2008, at 23:24, Daniel Kolbo wrote:

> Maciek Sokolewicz wrote:
>> Daniel Kolbo wrote:
>>> What is the preferred method with php to test and see if a file
>>> [pattern] exists?
>>>
>>> For example, i only need to search in one directory, that may have
>>> any number of files named such as afile1.txt, afile2.txt,
>>> afile3.txt, .... And also, bfile1.txt, bfile2.txt, bfile3.txt, ...
>>> I want to see if any such file 'family' exists. That is, i want
>>> to see if there is any file named bfile[1-9][0-9]+.txt. I don't
>>> care which bfile number exists, i just want to know if any bfile
>>> exists.
>>>
>>> I hope this is clear enough, if not let me know.
>>>
>>> thanks,
>>> dK
>>>
>>
>> glob()
>>
>> http://www.php.net/glob
> How portable is glob?
> How fast is glob? Being that it searches through the entire
> filesystem, this could potentially take a long time (like if i have
> wildcards early in the filepath pattern and lots of matches)
> correct? If my file variations (wildcards) are just at the end of
> of the filepaths and i don't have more than 1000 files in the
> directory then will I most likely be 'alright' with glob (in terms
> of time)? I have probably spent more time now 'considering' the
> time implications of glob, than glob actually would consume when
> operating...
>
> Thanks for the quick response/solutions.
> dK

Glob works on all platforms.

Glob does suffer from performance issues above a certain number of
files, and this can be system dependant. If you're unsure how many
files it may return you'd be better using opendir/readdir.

Not sure where you got the idea that glob searches the entire file
system, but it's limited to either the current working directory or
the directory you specify. So if your PHP file is in /var/www/htdocs
and you do glob('*.txt') you'll get all .txt files in /var/www/htdocs.
And if you do glob('/tmp/*.txt') you'll get all .txt files in /tmp.

-Stut

--
http://stut.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
German Geek
2008-12-10 01:08:25 UTC
Permalink
On Wed, Dec 10, 2008 at 1:13 PM, Stut <***@gmail.com> wrote:

>
> On 9 Dec 2008, at 23:24, Daniel Kolbo wrote:
>
> Maciek Sokolewicz wrote:
>>
>>> Daniel Kolbo wrote:
>>>
>>>> What is the preferred method with php to test and see if a file
>>>> [pattern] exists?
>>>>
>>>> For example, i only need to search in one directory, that may have any
>>>> number of files named such as afile1.txt, afile2.txt, afile3.txt, .... And
>>>> also, bfile1.txt, bfile2.txt, bfile3.txt, ...
>>>> I want to see if any such file 'family' exists. That is, i want to see
>>>> if there is any file named bfile[1-9][0-9]+.txt. I don't care which bfile
>>>> number exists, i just want to know if any bfile exists.
>>>>
>>>> I hope this is clear enough, if not let me know.
>>>>
>>>> thanks,
>>>> dK
>>>>
>>>>
>>> glob()
>>>
>>> http://www.php.net/glob
>>>
>> How portable is glob?
>> How fast is glob? Being that it searches through the entire filesystem,
>> this could potentially take a long time (like if i have wildcards early in
>> the filepath pattern and lots of matches) correct? If my file variations
>> (wildcards) are just at the end of of the filepaths and i don't have more
>> than 1000 files in the directory then will I most likely be 'alright' with
>> glob (in terms of time)? I have probably spent more time now 'considering'
>> the time implications of glob, than glob actually would consume when
>> operating...
>>
>> Thanks for the quick response/solutions.
>> dK
>>
>
> Glob works on all platforms.
>
> Glob does suffer from performance issues above a certain number of files,
> and this can be system dependant. If you're unsure how many files it may
> return you'd be better using opendir/readdir.
>
> Not sure where you got the idea that glob searches the entire file system,
> but it's limited to either the current working directory or the directory
> you specify. So if your PHP file is in /var/www/htdocs and you do
> glob('*.txt') you'll get all .txt files in /var/www/htdocs. And if you do
> glob('/tmp/*.txt') you'll get all .txt files in /tmp.
>
> -Stut
>
> --
> http://stut.net/
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
I wrote my own little function for a regex pattern match on files:
class FileHandle {
public static function copyReg($srcDir, $destDir, $regEx, $mkdir =
false) {
// ensure we have the right dir separator /(unix) \(win) and not at
the end
$srcDir = rtrim(str_replace(array('/','\\'), DIRECTORY_SEPARATOR,
$srcDir), DIRECTORY_SEPARATOR);
$destDir = rtrim(str_replace(array('/','\\'), DIRECTORY_SEPARATOR,
$destDir), DIRECTORY_SEPARATOR);
//echo "DEST: ". $destDir ." END";
if ($mkdir && !is_dir($destDir)) mkdir($destDir, 0777, true); //make
dir if not exists and mkdir
if ($handle = opendir($srcDir)) {
while (false !== ($file = readdir($handle))) {
//echo "$file\n";
preg_match($regEx, $file, $matches);
if ($file != '.' && $file != '..' && count($matches) > 0) {
//print("<pre>$regEx $srcDir $file \n=".
print_r($matches,true));
copy($srcDir . DIRECTORY_SEPARATOR . $file,
$destDir . DIRECTORY_SEPARATOR . $file);
}
}
return true;
}
return false;
}
}

Hope that helps. Don't know how good this will perform.


--
Tim-Hinnerk Heuer

http://www.ihostnz.com -- Web Design, Hosting and free Linux Support
c***@l-i-e.com
2008-12-09 23:16:49 UTC
Permalink
I'm not sure how glob works in the guts, but I know it is dog-slow for large numbers of files (or maybe just large numbers of results).

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Per Jessen
2008-12-10 07:39:13 UTC
Permalink
***@l-i-e.com wrote:

>
> I'm not sure how glob works in the guts, but I know it is dog-slow for
> large numbers of files (or maybe just large numbers of results).
>

I'm not sure what the context of this was, but the speed of searching a
directory with a large number of files, e.g. 100,000s, also depends a
lot on the filesystem.


/Per Jessen, Zürich


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...