Posted By

BilalELMoussaoui on 07/20/11


Tagged

php directory scandir scan


Versions (?)

scan directory


 / Published in: PHP
 

scandir function with more information about the files and folers like the size and mime type

  1. function scandirectorty($directory){
  2. if(is_dir($directory)){
  3. $directorys = glob($directory.'*');// show all the files from the folder
  4. $file = array();// new array
  5. $i = 0;
  6. foreach($directorys as $key => $value){
  7. if(is_file($value)){
  8. $file[$i]['name'] = basename($value); // get the file name
  9. $file[$i]['path'] = $value;// path
  10. $file[$i]['full_path'] = realpath($value);
  11. $file[$i]['size'] = (filesize($value) / 1024).'KB';// get the file size en kb
  12. $file[$i]['type'] = filetype($value);// get the type of file
  13. $file[$i]['extension'] = preg_replace('/^.*\.([^.]+)$/D', '$1', $value); // get the file extension
  14. if(function_exists('finof_open')){ // check if function exists
  15. if(version_compare(phpversion(),'5.3.0') >= 0){ // if php version more than or equals to 5.3.0
  16. $open = finfo_open(FILEINFO_MIME_TYPE);// open the mime type of the file
  17. }else{
  18. $open = finfo_open(FILEINFO_MIME); // open the mime type
  19. }
  20. $file[$i]['mime_type'] = finfo_file($open,$value);// get the mime type
  21. finfo_close($open); // close the finfo
  22. }else if(function_exists('mime_content_type')){// check if function exists
  23. $file[$i]['mime_type'] = mime_content_type($value); // get the mime type
  24. }
  25. $file[$i]['last_mod'] = date('d/m/Y h:i:s',filemtime($value));// get the last modification time
  26. $file[$i]['file_dir'] = dirname($value);// get the directory of the file
  27. }
  28. if(is_dir($value)){
  29. $file[$i]['dir'] = scand($value.'/');// scan the folder
  30. }
  31. $i++;
  32. }
  33. unset($directory,$directorys,$i);// unset the variables
  34. return $file;
  35. }else{
  36. trigger_error('Folder <strong>"'.$folder.'"</strong> Not exists',E_WARNING);// show an error if folder not exists
  37. }
  38. }

Report this snippet  

You need to login to post a comment.