can i make php to read directory listing, and each of the directory names to become a variable (it's linux based server)
or something like that?
can i make php to read directory listing
Try this:
if ( $dh = @opendir($pathToFile) )
{
while ( false !== ( $file = readdir($dh) ) )
{
if ( $file != '.' && $file != '..' )
{
$catch[] = $file;
}
}
}
All of your file names get stored in the array $catch.
it works perfect for my needs, thanks!
but i didn't understand good these two lines
if ( $dh = @opendir($pathToFile) )
{
while ( false !== ( $file = readdir($dh) ) )
if ( $dh = @opendir($pathToFile) )
{
while ( false !== ( $file = readdir($dh) ) )
In the above snippet from Zeb $dh will become a handle to the directory "$pathToFile".
The function readdir($directoryHandle) will return on each invocation a subdirectory of the directory $pathToFile. When there are no more directories to return the function returns false.
On a tidying up note the while loop checks for trueness (sorry for mangling the English language here). So the while loop could have been witten thus
while($file=readdir($dh))
{
... do some stuff
}
with the same results.
Originally posted by cwissy
while($file=readdir($dh)) { ... do some stuff }
Actually, that is the wrong way to loop through a directory. The expression in the parentheses after the while needs to test explicitly for the returned value of FALSE in order to properly terminate the loop. In your example, any directory whose name evaluates to FALSE (such as "0") will terminate the loop.
See the PHP manual page for readdir() for more info.
I know it's bulky, but the way I posted is correct.
You are quite right, and thanks for pointing that out. Late at night, I took one look at it without completely thinking it through.
Originally posted by cwissy
You are quite right, and thanks for pointing that out. Late at night, I took one look at it without completely thinking it through.
No problem