PHP dynamic directory listing with hyperlinks

I am using PHP in a Unix Solaris enviroment. I am a novice at best in php.
I have a list is file in say /logs.  I want the end-user to be able to view a list of files in the /logs directory, then with a hyperlink, be able to view the contents of the /logs/<filename> where say, file names would be a.log, b.log, c.log, and so forth.
How do I do this type of listing in PHP? I can build the basic html around the php, just not sure how to do the dynamic "array" portion, or maybe the array approach is not the best method?
Examples would be appreciated.
Any help?
bray007Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bray007Author Commented:
One more thing, for simplicity, please make file listing be filelisting.php, and the next "hyperlinked" output file to be filelistcontents.php
Thanks, Brian
BogoJokerCommented:
Hi bray007,

Sure, to get an array of the file listings try:
$path = '.'; // <-- this is the current directory, use a relative path here
$dir = opendir($path);  // <-- this would open a resource

This is directly from the php.net docs: http://us2.php.net/manual/en/function.readdir.php
List all files in the current directory and strip out .  and ..
<?php
if ($handle = opendir('.')) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != "..") {
           echo "$file\n";
       }
   }
   closedir($handle);
}
?>

Basically you would want to echo something other then $file,
probably something like:
echo "<a href=$file>$file</a><br>";
That way making it a link.

Be very careful with scripts like this, hopefully this is not public!

Joe P

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
frep45Commented:
As an addition to Joe's code, you can also limit what files you are displaying, in case you have other files you'd rather not advertise to be viewed (even if they are public).

In your example, you mentioned .log files...Here is a modification that will show only files ending with .log:

<?php
if ($handle = opendir('.')) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != ".." && false !== eregi(".*\.log$", $file)) {
           echo "$file\n";
       }
   }
   closedir($handle);
}
?>

Tom
bray007Author Commented:
I split the points because  Joe P provided me the correct direction, yet Tom provided the extra code that allowed me to do exacty what I needed to do, regaring the log files.  Thanks to both of you for the coding!!!!!!
bray007Author Commented:
Just in case anyone else needs to view the final code, here it is....
<?php


$mypath = '/var/apache/htdocs/logs/.'; // <-- this is the current directory, use a relative path here
$dir = opendir($mypath);  // <-- this would open a resource

if ($handle = $dir) {
   while (false !== ($myfile = readdir($handle))) {
       if ($myfile != "." && $myfile != "..") {
        
          echo "<a href=/logs/$myfile>$myfile</a><br>";

       }
   }
   closedir($handle);
}
echo "NEW STUFF";

?>
<?php
$mypath = '/var/apache/htdocs/logs/.'; // <-- this is the current directory, use a relative path here
$dir = opendir($mypath);  // <-- this would open a resource
if ($handle = $dir) {
   while (false !== ($myfile = readdir($handle))) {
       if ($file != "." && $myfile != ".." && false !== eregi(".*\.txt$", $myfile)) {
           //echo "$myfile\n";
               echo "<a href=/logs/$myfile>$myfile</a><br>";
       }
   }
   closedir($handle);
}
?>
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.