note 99152 modified in function.glob by danbrown

From: Date: Fri, 30 Jul 2010 16:27:07 +0000
Subject: note 99152 modified in function.glob by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-171237@lists.php.net to get a copy of this message
If you need to retrieve files that are in another folder than your PHP script and want to use glob because you think it's shorter than the oldfashioned readdir loop, forget about it. Let's say your script is in the base folder of your webspace and you have a folder called "myfolder" in it, from which you want to retrieve all files into an array. The result will be something like: myfolder/this.txt myfolder/that.txt myfolder/something.txt myfolder/blah.txt As you can see, you will have to write an additional loop to get rid of "myfolder/" and have the filenames alone, so you might as well use: <?php if ($foldername = opendir("myfolder")) { while (false !== ($filename = readdir($foldername))) { if ($filename != "." && $filename != "..") { $files[] = $filename; } } closedir($foldername); } ?> Which gives you: this.txt that.txt something.txt blah.txt --was-- If you need to retrieve files that are in another folder than your PHP script and want to use glob because you think it's shorter than the oldfashioned readdir loop, forget about it. Let's say your script is in the base folder of your webspace and you have a folder called "myfolder" in it, from which you want to retrieve all files into an array. The result will be something like: myfolder/this.txt myfolder/that.txt myfolder/something.txt myfolder/blah.txt As you can see, you will have to write an additional loop to get rid of "myfolder/" and have the filenames alone, so you might as well use: if ($foldername = opendir("myfolder")) { while (false !== ($filename = readdir($foldername))) { if ($filename != "." && $filename != "..") { $files[] = $filename; } } closedir($foldername); } Which gives you: this.txt that.txt something.txt blah.txt http://php.net/manual/en/function.glob.php

« previous php.notes (#171237) next »