note 66934 deleted from function.call-user-func by bjori

From: Date: Thu, 01 Jun 2006 13:00:02 +0000
Subject: note 66934 deleted from function.call-user-func by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-112748@lists.php.net to get a copy of this message
Note Submitter: Jayme Rotsaert <rotsaert<AT>gmail.com> Reason: useless ---- Here's a handy function for reading directories (recursive). Works under all PHP versions. <?php class FileHandler{ function readDirectory( $directory, $callbackFunction, $listDirectories=false, $recursiveRead=false, $allowedExtensions=false) { if ($directory = FileHandler::fullPath( $directory, true ) ){ if ($handle = opendir($directory)) { if(isset($callbackFunction[2])) { $args = $callbackFunction[2]; unset($callbackFunction[2]); } while (false !== ($file = readdir($handle))) { if ($file !== '.' && $file !== '..' ) { $passObject = pathinfo($directory.$file); if(isset($args)) { $passObject['args'] = $args; } if(is_dir($directory.$file)){ if($listDirectories){ $passObject['path']= $passObject['dirname'].'/'. $passObject['basename'].'/'; call_user_func($callbackFunction,$passObject); } if($recursiveRead){ FileHandler::readDirectory( $directory.$file, $callbackFunction, $listDirectories, $recursiveRead, $allowedExtensions); } }elseif(is_file($directory.$file)){ $passObject['path']= $passObject['dirname'].'/'. $passObject['basename']; $passObject['extension'] = array_pop(explode('.', $passObject['path'])); $passObject['filename'] = basename($passObject['path'], '.'.$passObject['extension']); if( is_array($allowedExtensions) && in_array($passObject['extension'],$allowedExtensions) ){ call_user_func($callbackFunction,$passObject); } elseif ( $allowedExtensions !== false && strtolower($passObject['extension']) === strtolower($allowedExtensions) ) { call_user_func($callbackFunction,$passObject); } else { call_user_func($callbackFunction,$passObject); } } } } closedir($handle); } } } function fullPath($directory,$errCheck=false) { $directory = realpath($directory); if(substr($directory,-1,1)!=='/') { $directory .= '/'; } return $directory; } } ?> usage: (assuming that your public dir has a images directory) -> create a thumbnail from all 'gif','jpg','png' images in the images folder FileHandler::readDirectory( 'images', array('ImageController','thumbnail'), false, true, array('gif','jpg','png') ); -> list all files & directories under a certain directory FileHandler::readDirectory( 'joey', array('FileController','userFiles'), true, true ); Notes: ------- Call the function with a reference to this or another object: FileHandler::readDirectory( 'test', array('Controller','controllerFunction', $this) ); in the controllerFunction you can access the passed object through: $objectPassed['args']; The callback function should be in this format: function aCallbackfunction($objectPassed){ // do stuff with the object passed; }

« previous php.notes (#112748) next »