Page 1 of 1

directory lister

Posted: Sat Jan 22, 2005 1:23 pm
by jtd
Hi,
I want to list all .jpg files in some directory and put result on page. When I run script using http://site.bogus/script.php it works fine. But when im triyng to run it using a {php} include("/whole/dir/script.php"); {/php} it dont give me list of files but ony write
"image(s) found in this directory.". How to repair it? Anybody have idea? Thanks in advance.

This is code of my directory lister.

Code: Select all

<?
function CheckExt($filename, $ext) {
$passed = FALSE;
$testExt = "\.".$ext."$";
if (eregi($testExt, $filename)) {
$passed = TRUE;
}
return $passed;
}

//Define an array of common extensions.
$exts = array("gif","jpg$|\\.jpeg","png","bmp");

//echo "<b>Images in this folder:</b><br>";
$dir = opendir(".");
$files = readdir($dir);

while (false !== ($files = readdir($dir))) {
foreach ($exts as $value) {
if (CheckExt($files, $value)) {
echo "<a href=\"$files\">$files</a>\n<br>";
$count++; //Keep track of the total number of files.
break; //No need to keep looping if we've got a match.
}
}

}
echo $count." image(s) found in this directory.\n";
closedir($dir);

?>

directory lister

Posted: Sat Jan 22, 2005 1:28 pm
by Ted
Is the script.php in the same dir as all of the files?

directory lister

Posted: Sat Jan 22, 2005 2:33 pm
by jtd
script.php is in the directory with jpg files

simple:
script.php is in /home/user/www/test
url is www.homewww.com/test

i can run www.homewww.com/test/script.php in result i get listed jpgs but when im tring to do the same action but using include it doesnt work

directory lister

Posted: Sat Jan 22, 2005 2:56 pm
by Ted
Try changing

Code: Select all

$dir = opendir("."); 
to

Code: Select all

$dir = opendir(dirname(__FILE__));
Just in case there is a weird directory issue.

directory lister

Posted: Sun Jan 23, 2005 10:57 am
by Anonymous
OMG, wishy now it works :) Thanks for your help.