[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 462
  • Last Modified:

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?
0
bray007
Asked:
bray007
  • 3
2 Solutions
 
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
0
 
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
0
 
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
0
 
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!!!!!!
0
 
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);
}
?>
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now