On Wed, Dec 10, 2008 at 1:13 PM, Stut <stuttle@xxxxxxxxx> 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