PHPEdit.net Community
   
1 2 3 4
Tutorials Tips Pasties Code Snippets
 

Home > Code snippets > PHP > File operations class (still under progress)

File operations class (still under progress)

Created by Jose Chafardet, last update on 30/10/2008 11:37

This is a small class i have been writing to perform several file operations. for now only list files on a single directory and the same but recursively. I will keep improving it and modifying this one here as I work on it.

  1. //==================
  2. // Define class name
  3. class Fileops {
  4. //===========
  5. // Properties
  6. var $path;
  7. var $farray;
  8. //===========================
  9. // Public method get_filelist
  10. // Gets a list of files (non recursive)
  11. // contained on a given path
  12. public function get_filelist ( $path ) {
  13. $this->farray = array();
  14. $this->path = $path . '/';
  15. // get the array of values on the $path given, removing . and ..
  16. $this->farray = scandir($this->path);
  17. // for every value on the array $array, as $item, check if any is a
    directory
  18. foreach($this->farray as $item) {
  19. if ( ( $item != '.' ) && ( $item != '..' ) ) {
  20. if ( is_file ( $this->path . $item ) ) {
  21. $return_array[] = $item;
  22. } // end if
  23. } // end if
  24. } // end foreach
  25. return $return_array;
  26. } // end public method get_filelist
  27. //===========================
  28. // Public method get_rfilelist
  29. // like get_filelist, gets a list of files
  30. // on a given path, but recursive.
  31. function get_rfilelist ( $path, &$return_array=array() ) {
  32. $this->path = $path . '/';
  33. // get the array of values on the $path given, removing . and ..
  34. $array = scandir($path);
  35. // for every value on the array $array, as $item, check if any is a
    directory
  36. foreach($farray as $item) {
  37. if ( ( $item != '.' ) && ( $item != '..' ) ) {
  38. if ( is_dir ( $this->path . $item ) ) {
  39. $return_array[] = $item;
  40. $item = $this->path . $item . "/";
  41. $return_array = $this->get_rfilelist($item, $return_array);
  42. }
  43. else if ( is_file ( $this->path . $item ) ) {
  44. $return_array[] = $item;
  45. }
  46. }
  47. }
  48. return $return_array;
  49. }
  50. } // end class

Dependencies

No special requirements are needed by this snippet

By logging in you will be able to:

  • Recommand this page to someone else
  • Monitor changes on this item
  • Rate this item
  • Post comments
  • Download this item
Login now!

 
PHPEdit User Community, © 2008 WaterProof SARL
Powered by PHPEdit