dir_callback - Lets play with Directories (PHP)

Web-hosting business can now be started for just $19.95 with unlimited resources. Start your own.

PHP provides some handy functions for file handling, but to deal with directories, it does not provide much. It has basic directory functions and a directory iterator, but does not cover directory level operations like moving, cloning, counting files, deleting etc.

Lets try it ourself: Here we have a very simple directory function that takes a path and iterates through all its files and sub-directories (nodes) and passes on path of each node to the callback function.

  1. function dir_callback( $dir , $callback , $args = array( ) )
  2. {
  3.     $dir    = realpath( $dir );
  4.     if( $handle = opendir( $dir ) )
  5.     {
  6.         while( false !== ( $dir_item = readdir( $handle ) ) )
  7.         {
  8.             if( !( $dir_item === '..' || $dir_item === '.' ) )
  9.             {
  10.                 $dir_item_path  = $dir . DIRECTORY_SEPARATOR . $dir_item;
  11.                
  12.                 $callback( $dir_item_path , $args );
  13.                
  14.                 if( is_dir( $dir_item_path ) )
  15.                 {
  16.                     dir_callback( $dir_item_path , $callback , $args );
  17.                 }
  18.             }
  19.         }
  20.         closedir( $handle );
  21.     }
  22. }
PHP Code

Using the above simple function, we can do much of what we want. This function does nothing, except to pass the full path of each node (directory or file) to the callback function. The callback function then decides on how to use that path. Notice that the function even does not return anything. It does not force you to use a fixed way to store data in recursive calls. You have to define in your callback your own way to store data. For simplicity, I am using
$GLOBALS
. Alternatively you may modify it to store data in a
static
variable, arrays, or write your own data storage/retrieval class.

Here I have written some callback functions to be used in combination with dir_callback.

Calculating Directory Size (in Bytes)

  1. function dir_size( $path )
  2. {
  3.     if( is_file( $path ) )
  4.     {
  5.         $GLOBALS['filesize']    += filesize( $path );
  6.     }
  7. }
  8.  
  9. $path     = realpath( 'E:\files' );
  10. $filesize = 0;
  11. dir_callback( $path , 'dir_size' );
  12.  
  13. //echo $filesize . ' Bytes';
PHP Code

List all Files and Sub-Directories

  1. function dir_list( $path )
  2. {
  3.     $GLOBALS['files'][] = $path;
  4. }
  5.  
  6. $path    = realpath( 'E:\files' );
  7. $files   = array( );
  8. dir_callback( $path , 'dir_list' );
  9.    
  10. //print_r( $files );
PHP Code

Get Directory Structure

The above callback function returns all files and directories inside the parent directory. Here we have a function that returns an array of sub-directories only, constituting the whole directory structure.
  1. function dir_structure( $path )
  2. {
  3.     if( is_dir( $path ) )
  4.     {
  5.         $GLOBALS['files'][] = $path;
  6.     }
  7. }
  8.  
  9. $path    = realpath( 'E:\files' );
  10. $files   = array( );
  11. dir_callback( $path , 'dir_structure' );
  12.  
  13. //print_r( $files );
PHP Code

Styling Paths and File Names

This function will color the file name and the file path separately. Similar styling is used by many online storage services to display easy to read file names in deep nested file paths.
  1. function dir_style( $path )
  2. {
  3.     $last_ds    = strrpos( $path , DIRECTORY_SEPARATOR );
  4.     $GLOBALS['files']   .= '<span style="color:#999;">'.substr( $path , 0 , $last_ds ).'</span>'.
  5.                             '<span style="color:#000;">'.substr( $path , $last_ds ).'</span><br />';
  6. }
  7.  
  8. $path    = realpath( 'E:\files' );
  9. $files   = array( );
  10. dir_callback( $path , 'dir_style' );
  11.  
  12. //print_r( $files );
PHP Code

List all files by Extension

  1. function dir_list_by_ext( $path )
  2. {
  3.     if( is_file( $path ) )
  4.     {
  5.         $GLOBALS['files'][pathinfo( $path , PATHINFO_EXTENSION )][] = $path;
  6.     }
  7. }
  8.  
  9. $path    = realpath( 'E:\files' );
  10. $files   = array( );
  11. dir_callback( $path , 'dir_list_by_ext' );
  12.  
  13. //print_r( $files );
PHP Code

Count Files and Directories

  1. function dir_count( $path )
  2. {
  3.     if( is_file( $path ) )
  4.     {
  5.         $GLOBALS['files']['file'] = ( isset( $GLOBALS['files']['file'] ) ? $GLOBALS['files']['file'] + 1 : 1 );
  6.     }
  7.    
  8.     if( is_dir( $path ) )
  9.     {
  10.         $GLOBALS['files']['dir'] = ( isset( $GLOBALS['files']['dir'] ) ? $GLOBALS['files']['dir'] + 1 : 1 );
  11.     }
  12. }
  13.  
  14. $path    = realpath( 'E:\files' );
  15. $files   = array( );
  16. dir_callback( $path , 'dir_count' );
  17.  
  18. //print_r( $files );
PHP Code

Getting List of Unique File Names

This functions lists unique names of all files in the given directory.
  1. function dir_filenames( $path )
  2. {
  3.     if( is_file( $path ) )
  4.     {
  5.         $filename   = basename( $path );
  6.         $GLOBALS['files'][$filename]    = true;
  7.     }
  8. }
  9.  
  10. $path    = realpath( 'E:\files' );
  11. $files   = array( );
  12. dir_callback( $path , 'dir_filenames' );
  13.  
  14. //print_r( $files );
PHP Code

Get Duplicate Files (Using File Names)

This function uses file names to identify duplicate at any level within a directory, and returns a list of such file paths.
  1. function dir_get_duplicates_by_filename( $path )
  2. {
  3.     dir_callback( $path , function( $path ) {
  4.         if( is_file( $path ) )
  5.         {
  6.             $filename   = basename( $path );
  7.             $GLOBALS['files'][$filename][]  = $path;
  8.         }
  9.     } );
  10.    
  11.     $GLOBALS['files']   = array_map( function ( $e ) {
  12.                     if( count( $e ) > 1 )
  13.                     {
  14.                         return $e;
  15.                     }
  16.                 } , $GLOBALS['files'] );
  17.     $GLOBALS['files']   = array_filter( $GLOBALS['files'] );
  18. }
  19.  
  20. $path    = realpath( 'E:\files' );
  21. $files   = array( );
  22. dir_get_duplicates_by_filename( $path );
  23.  
  24. //print_r( $files );
PHP Code

Get Duplicate Files (Using MD5)

This function uses
md5
of all files within a directory (at any level) to identify duplicates, and returns a list of such duplicates.
  1. function dir_get_duplicates_by_md5( $path )
  2. {
  3.     dir_callback( $path , function( $path ) {
  4.         if( is_file( $path ) )
  5.         {
  6.             $md5    = md5( file_get_contents( $path ) );
  7.             $GLOBALS['files'][$md5][]   = $path;
  8.         }
  9.     } );
  10.    
  11.     $GLOBALS['files']   = array_map( function ( $e ) {
  12.                     if( count( $e ) > 1 )
  13.                     {
  14.                         return $e;
  15.                     }
  16.                 } , $GLOBALS['files'] );
  17.     $GLOBALS['files']   = array_filter( $GLOBALS['files'] );
  18. }
  19.  
  20. $path    = realpath( 'E:\files' );
  21. $files   = array( );
  22. dir_get_duplicates_by_md5( $path );
  23.  
  24. //print_r( $files );
PHP Code

Clone a Directory (Creating a Copy of Directory)

  1. function dir_clone( $path , $args )
  2. {
  3.     $source_base_len    = strlen( $args['from'] );
  4.    
  5.     $dest_path  = substr_replace( $path , $args['to'] , 0 , $source_base_len );
  6.    
  7.     if( is_dir( $path ) )
  8.     {
  9.         mkdir( $dest_path , 0777 );
  10.     }
  11.     else if( is_file( $path ) )
  12.     {
  13.         copy( $path , $dest_path );
  14.     }
  15. }
  16.  
  17. $path   = realpath( 'E:\files' );
  18. dir_callback( $path , 'dir_clone' , array( 'from' => $path , 'to' => 'E:\More_Files' ) );
PHP Code

Truncate a Directory but Preserve Directory Structure

  1. function dir_empty( $path )
  2. {
  3.     if( is_file( $path ) )
  4.     {
  5.         unlink( $path );
  6.     }
  7. }
  8.  
  9. $path    = realpath( 'E:\files' );
  10. $files   = array( );
  11. dir_callback( $path , 'dir_empty' );
  12.  
  13. //print_r( $files );
PHP Code

List all files by their Sizes

  1. function dir_list_by_size( $path )
  2. {
  3.     if( is_file( $path ) )
  4.     {
  5.         $GLOBALS['files'][filesize( $path )][]  = $path;
  6.     }
  7. }
  8.  
  9. $path    = realpath( 'E:\files' );
  10. $files   = array( );
  11. dir_callback( $path , 'dir_list_by_size' );
  12.  
  13. //print_r( $files );
PHP Code

List all Files by their Last Modified Time

  1. function dir_list_by_lastmodified( $path )
  2. {
  3.     if( is_file( $path ) )
  4.     {
  5.         $GLOBALS['files'][filemtime( $path )][] = $path;
  6.     }
  7. }
  8.  
  9. $path    = realpath( 'E:\files' );
  10. $files   = array( );
  11. dir_callback( $path , 'dir_list_by_lastmodified' );
  12.  
  13. //print_r( $files );
PHP Code

List all Files by their Last Access Time

  1. function dir_list_by_lastaccess( $path )
  2. {
  3.     if( is_file( $path ) )
  4.     {
  5.         $GLOBALS['files'][fileatime( $path )][] = $path;
  6.     }
  7. }
  8.  
  9. $path    = realpath( 'E:\files' );
  10. $files   = array( );
  11. dir_callback( $path , 'dir_list_by_lastaccess' );
  12.  
  13. //print_r( $files );
PHP Code

All the above functions are useful for operations like Disk Cache Management, Uploads Management, User Account Deleting (Deleting all uploaded data of user), optimizing file system usage (by removing duplicates), summarized reporting direct from file system, etc



  1. High Performance Hosting is now $3.96 a month


© 2012-2017 PageConfig.com - Scripts - Twitter