Directory Listing and Link Generator

  • Thread starter Thread starter Anonymous
  • Start date Start date
A

Anonymous

Guest
Hello,

I am fairly new to PHP though i have several years of HTML, and CSS. I am attempting to have my php code read a directory on my server and then spit the results of the directory contents back out as genrated links on the pages. The code is simple and i have implemented it but it only seems to be semi working. Anybody possible can help?

Code being used (Partial, only that related to the issue)

<?php
$dir = "/assets/files/";

// Open a known directory, and generate the download links.
if (is_dir($dir)) {
if ($db = opendir($dir)) {
while (($file = readdir($db)) !== false) {
if ($file != "." && $file != "..") {
$dblisting .= '<li><a href="'.$file.'">'.$file.'</a></li>';
}

}
closedir($db);
}
}
?>

<h1>Downloads Available:</h1>
<ul><?php echo $dblisting; ?></ul>


And i have included a snapshot of what the finished product comes out as. You'll notice that while it shows the H1 tag of the code it does not php echo the $dblisting.

*NOTE* The blue mark out is merely me marking out a portion of my footer.

Capture.PNG
 
You haven't explained the 'only seems to be semi working'.

I suspect that the file location is incorrect; try:
Code:
$dir = $_SERVER['DOCUMENT_ROOT'] . "/assets/files/";
 
Hyper,
Thanks for the reply. I really do appreciate it. As for the semi working it seems only the wrapped HTML code is working. It spits out the <h1> Downloads Available</h1> but none of the php which is wired as the php code worked fine until I link the stylesheet to it. I will try your suggestion and hope it works.
 
hyper said:
You haven't explained the 'only seems to be semi working'.

I suspect that the file location is incorrect; try:
Code:
$dir = $_SERVER['DOCUMENT_ROOT'] . "/assets/files/";

Hyper,
When i replied last i had actually forgotten that this PHP was designed to spit out everything that is in the directory it is stored in. Meaning you place the page this PHP is in the directory that you want listed out while it also omits any file with a "." in front of it. I actually don't like the way that PHP coding works, i find it amaturish but i can not seem to get the PHP to do what i want.
 
it also omits any file with a "." in front of it
That has nothing to do with PHP, it's down to the operating system as it's how files are hidden. Providing you haven't enabled 'Show hidden files', you can rename a file with the dot in front, and it will disappear from your file manager listing.

While PHP may have a few quirks, it is significantly less than the how browsers interpret HTML and CSS.

OK, I don't tend to look at code unless it's formatted correctly and in a code box, with time on my hands - you have primarily fallen victim to the scope rules of variables, all languages have scope in one form or another, $dblisting is first defined in a while loop, and that is its scope, so it doesn't exist outside of that scope to be displayed.

Code:
<?php
$dir = "/assets/files/";

$dblisting = ""; # define $dblisting

// Open a known directory, and generate the download links.
if (is_dir($dir)) {
  if ($db = opendir($dir)) {
    while (($file = readdir($db)) !== false) {
      if ($file != "." && $file != "..") {
        $dblisting .= '<li><a href="' . $file . '">' . $file . '</a></li>';
      }
    }
    closedir($db);
  }
}
?>

<h1>Downloads Available:</h1>
<ul>
<?php
echo $dblisting;
?>
</ul>
this has been tested, so if it doesn't work for you, look at $dir = ...
 
Ok i have used every combination and every fix i can think of it but none of it is working. I've rewritten the script many times, consulted with PHP manual and can not get it to work. I literally just want my script to read my download directory and spit out the directories and files within as downloadable links. Any suggestions?
 
I am using a local webserver. I don't have anything posted or published publicly. I have a directory where all my downloadable files are. I want the code to read the entire tree and list it as downloadable links. I can achieve this using regular HTML and HTACCESS, but that looks so gaudy. I've pretty much given up on it. I think it's my coding skills. I'm not too skilled with PHP.
 
@Hyper,

I pretty much gave up on it. I apologize for such a late response! I had something come up and I've been out of the game for awhile. Thank you for your help.
 
Back
Top