Tuesday, August 12, 2008

Alternative solution for scandir() function in PHP 4

In PHP 5 there is a new function scandir() which lists files and directories inside a path. In this case if you install your script, in a PHP 4 server, which uses scandir() function, then you will notice following error message:

Call to undefined function: scandir()

To solve this problem just add following code in your function page. This will make your script available in both PHP 4 & PHP 5 server.

if(!function_exists('scandir')) {
function scandir($folder) {
$handle = opendir($folder);
while (false !== ($filename = readdir($handle))) {
$files[] = $filename;
}
return $files;
}
}

Bookmark It!
Subscribe in a reader

2 comments:

uci said...

I've coming from embedded programming with C, and be in the second or third steps with php. Thus help you've given is constructive and helps to master the big amount of php functions, whch run on the local server but not in case of the online account, due to php4 installation.
Your scrip works very good.
Thank you.

Unknown said...

Its a right way solve this problem in PHP . thanks for sharing all about.