Posted By

chrisaiv on 12/28/09


Tagged

php scandir opendir Readdir


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

BrianCoyDesign
lifewishes


PHP: List out items in a directory


 / Published in: PHP
 

See the contents within a directory and print them out.

  1. $dir = ".";
  2.  
  3. if( is_dir( $dir ) ){
  4. if( $dir_handle = opendir( $dir ) ){
  5. while( $filename = readdir( $dir_handle ) ){
  6. echo "filename: {$filename}<br />" ;
  7. }
  8. //rewinddir($dir_handle) to start over
  9. closedir( $dir_handle );
  10. }
  11. }
  12.  
  13. echo "<hr />";
  14.  
  15. if( is_dir( $dir ) ){
  16. $dir_array = scandir( $dir );
  17. foreach( $dir_array as $file ){
  18. if( stripos( $file, '.' ) > 0){
  19. echo "filename: {$file}<br />";
  20. }
  21. }
  22. }

Report this snippet  

You need to login to post a comment.