John Davidson

How to make the file list in a directory look cleaner in html/php?

0 comments
Message:


I have a directory that houses pdf files housed in a mounted external drive on a localhost Raspberry Pi webserver. I'm able to get the vanilla "index of /pdfs" to show the files as hyperlinks, but it's not terribly aesthetically pleasing. I've read that there is a way to create a php script that essentially does the same thing with an opendir() function, but aside from the style.css, I'm unable to get the file list to come up. I've tried multiple different methods for the argument in opendir(), but can't seem to get the files to show up. Any ideas what I may be doing wrong? The OS is Ubuntu Desktop so I'm doing this in Sublime and the terminal. I'm putting the php page inside the directory with the pdfs I want to display the links to but not sure if that is the place it is supposed to go. Thanks in advance for your help!



My Answer:
To create a cleaner file list in a directory using PHP, you can use the following code:

1. Create a new PHP file in the directory where your PDF files are located. You can name it something like "filelist.php".

2. Add the following code to the "filelist.php" file:

php



File List



File List



    $dir = opendir('.');
    while (($file = readdir($dir)) !== false) {
    if ($file != '.' && $file != '..' && !is_dir($file)) {
    echo '
  • ' . $file . '
  • ';
    }
    }
    closedir($dir);
    ?>





3. Save the file and access it through your web browser by navigating to the URL of the file (e.g., http://localhost/pdfs/filelist.php).

This code will create a simple HTML page with a list of all the files in the directory as clickable hyperlinks. You can customize the styling of the list by modifying the CSS styles in the `