PHP function scan_dir();

January 23rd, 2007 by Quatrux

As a lot of who knows,PHP5 has a very good function called scan dir, but ash many of use still like to use PHP4 we need to find aletrnatives for it so here is a good function I wrote years ago and want to share it with you :)

  1. /* #### SCAN DIR ####
  2. Input: Directory, 1 to rsort() and files to not index to the Array
  3. Output: makes an Array with filenames and dirs on Success or False
  4. Status: Complete
  5. Last Editor: Quatrux
  6. */
  7.  
  8. function scan_dir($dir, $s = ‘0′, $u = ) {
  9. /* Check if input is a Directory and Open it */
  10. if (is_dir($dir)) {
  11. if ($d[‘open’] = opendir($dir)) {
  12. $u = "**.|..|$u|";
  13. while (($d[‘file’] = readdir($d[‘open’])) !== false) {
  14. /* Don’t add unneeded Files */
  15. if (!strpos($u, $d[‘file’]) ) {
  16. /* Make an array with all left Files */
  17. $d[‘array’][] = $d[‘file’];
  18. }
  19. }
  20. closedir($d[‘open’]);
  21. } else {
  22. return FALSE;
  23. }
  24. } else {
  25. return FALSE;
  26. }
  27. /* Check if Directory is not Empty */
  28. if (!isset($d[‘array’])) {
  29. return FALSE;
  30. /* Sort the Array by Request */
  31. } elseif (is_array($d[‘array’])) {
  32. if ($s == ‘1′) {
  33. rsort($d[‘array’]);
  34. } else {
  35. sort($d[‘array’]);
  36. }
  37. }
  38. return $d[‘array’];
  39. }

Well, will need to find a plugin or something which would highlight the code or something, because now, I don’t even see tabs, oh well.. Stupid Javascript Editor for Wordpress ;)

Posted in PHP | No Comments »

Leave a Comment



Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment and don't be afraid of the spam protection!