Re: [PHP] getting filenames from dir
Try this function: function scandir($dirstr){ $files = array(); $fh = opendir($dirstr); while (false !== ($filename = readdir($fh))){ array_push($files, $filename);} closedir($fh); return $files; } return an array with all the files in an array... or use scandir() in PHP5 :-) regards jesper Merlin wrote: Hi there, does anybody know how to get all file names from a specified directory into an array? Thanx, Merlin
RE: [PHP] getting filenames from dir
HI... Do you read php manual sometimes? ;o) just replace echo $file\n; by $my_file_array[]=$file; readdir (PHP 3, PHP 4 , PHP 5) readdir -- read entry from directory handle Description string readdir ( resource dir_handle) Returns the filename of the next file from the directory. The filenames are returned in the order in which they are stored by the filesystem. Please note the fashion in which readdir()'s return value is checked in the examples below. We are explicitly testing whether the return value is identical to (equal to and of the same type as--see Comparison Operators for more information) FALSE since otherwise, any directory entry whose name evaluates to FALSE will stop the loop (e.g. a directory named 0). Example 1. List all files in a directory ?php // Note that !== did not exist until 4.0.0-RC2 if ($handle = opendir('/path/to/files')) { echo Directory handle: $handle\n; echo Files:\n; /* This is the correct way to loop over the directory. */ while (false !== ($file = readdir($handle))) { echo $file\n; } /* This is the WRONG way to loop over the directory. */ while ($file = readdir($handle)) { echo $file\n; } closedir($handle); } ? Note that readdir() will return the . and .. entries. If you don't want these, simply strip them out: Example 2. List all files in the current directory and strip out . and .. ?php if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != . $file != ..) { echo $file\n; } } closedir($handle); } ? -Original Message- From: Merlin [mailto:[EMAIL PROTECTED] Sent: 01 April 2005 12:32 To: php-general@lists.php.net Subject: [PHP] getting filenames from dir Hi there, does anybody know how to get all file names from a specified directory into an array? Thanx, Merlin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] getting filenames from dir
And does one manage recursive sub directories for the same thing ? On Apr 1, 2005 3:38 AM, Stanislav Kuhn [EMAIL PROTECTED] wrote: HI... Do you read php manual sometimes? ;o) just replace echo $file\n; by $my_file_array[]=$file; readdir (PHP 3, PHP 4 , PHP 5) readdir -- read entry from directory handle Description string readdir ( resource dir_handle) Returns the filename of the next file from the directory. The filenames are returned in the order in which they are stored by the filesystem. Please note the fashion in which readdir()'s return value is checked in the examples below. We are explicitly testing whether the return value is identical to (equal to and of the same type as--see Comparison Operators for more information) FALSE since otherwise, any directory entry whose name evaluates to FALSE will stop the loop (e.g. a directory named 0). Example 1. List all files in a directory ?php // Note that !== did not exist until 4.0.0-RC2 if ($handle = opendir('/path/to/files')) { echo Directory handle: $handle\n; echo Files:\n; /* This is the correct way to loop over the directory. */ while (false !== ($file = readdir($handle))) { echo $file\n; } /* This is the WRONG way to loop over the directory. */ while ($file = readdir($handle)) { echo $file\n; } closedir($handle); } ? Note that readdir() will return the . and .. entries. If you don't want these, simply strip them out: Example 2. List all files in the current directory and strip out . and .. ?php if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != . $file != ..) { echo $file\n; } } closedir($handle); } ? -Original Message- From: Merlin [mailto:[EMAIL PROTECTED] Sent: 01 April 2005 12:32 To: php-general@lists.php.net Subject: [PHP] getting filenames from dir Hi there, does anybody know how to get all file names from a specified directory into an array? Thanx, Merlin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] getting filenames from dir
That should get you started : $dirname='/var'; $dh=opendir($dirname) or die('could not open directory'); while (!(($file=readdir($dh))===false)){ if (is_dir('$dirname/$file')){ $myarray[]=$file; } echo $filebr; $myarray[]=$file; } Mario Merlin wrote: Hi there, does anybody know how to get all file names from a specified directory into an array? Thanx, Merlin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php