directory lister Topic is solved

General project discussion. NOT for help questions.
Post Reply
jtd

directory lister

Post 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);

?>
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm
Location: Fairless Hills, Pa USA

directory lister

Post by Ted »

Is the script.php in the same dir as all of the files?
jtd

directory lister

Post 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
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm
Location: Fairless Hills, Pa USA

directory lister

Post 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.
Anonymous

directory lister

Post by Anonymous »

OMG, wishy now it works :) Thanks for your help.
Post Reply

Return to “General Discussion”