note 66934 added to function.call-user-func
| From: | JaymeRotsaert<rotsaert" gmail . com | Date: | Thu, 01 Jun 2006 10:53:45 +0000 |
| Subject: | note 66934 added to function.call-user-func | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-112736@lists.php.net to get a copy of this message | ||
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;
}
----
Server IP: 193.190.198.20
Probable Submitter: 83.101.15.27
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.call-user-func.php
Edit -- http://master.php.net/note/edit/66934
Del: integrated -- http://master.php.net/note/delete/66934/integrated
Del: useless -- http://master.php.net/note/delete/66934/useless
Del: bad code -- http://master.php.net/note/delete/66934/bad+code
Del: spam -- http://master.php.net/note/delete/66934/spam
Del: non-english -- http://master.php.net/note/delete/66934/non-english
Del: in docs -- http://master.php.net/note/delete/66934/in+docs
Del: other reasons-- http://master.php.net/note/delete/66934
Reject -- http://master.php.net/note/reject/66934
Search -- http://master.php.net/manage/user-notes.php