faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

28 of 42 people (67%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How to READ a directory of PDF files that display as links to open the pdf

Feb 28th, 2008 00:10
dman, Chris Carlton, Ken Freeman, http://www.ttnr.org


<?php
#######################################################################
#######
  # Project : Reading and Linking to files in specified 
directories            #
  # Name    : Chris 
Carlton                                                    #
  # Email   : 
[email protected]                                      #
  # Date    : Sept 3, 
2002                                                     #
  # Comments: This script will read a directory for a particular file 
type and #
  #           generate links to the files themselves. It will also 
display the #
  #           the file size to inform 
users.                                   #
#######################################################################
#######
  # SET THIS TO YOUR PDF DIRECTORY...
  $TheDirectory 
=  "/var/www/html/scripts/files/";
  # SET THIS TO THE EXTENSION OF FILE YOU WANT TO LIST AND LINK TO
  $TheFileType = "pdf";
  # YOU CAN SET THIS TO POINT DIRECTLY AT THE FILES OTHERWISE THE 
SCRIPT WILL GENERATE THE LINK FOR YOU
  #$LinkTo 
= "http://www.your-domain-name.com/files/";
  function ListFiles($FileType, $Directory, $LinkTo = false) {
    # GLOBALISE WHERE WE ARE!
    GLOBAL $PHP_SELF;
    # Initialize temporary array for sorting.
    $DirectoryFiles = array();
    # Print current directory location.
    printf ("<h3>%s</h3>\n", $Directory);
    # Change to directory.
    chdir($Directory);
    # Open directory.
    $DirectoryHandle = @opendir($Directory) or die("The directory 
\"$Directory\" was not found.");
    # Loop through the directory entries.
    while($DirectoryEntry = readdir($DirectoryHandle)) {
      if ((is_dir($DirectoryEntry)) && ($DirectoryEntry != "..") && 
($DirectoryEntry != ".")) {
        # we don't want this but you could do something with it later 
on!!
      } elseif (($DirectoryEntry != "..") && ($DirectoryEntry != ".") 
&& (substr(strtolower($DirectoryEntry), strrpos($DirectoryEntry, ".")
+1))) {
        $DirectoryFiles[] = $DirectoryEntry;
      }
    }
    # Sort the directory files into alphabetical order before 
displaying.
    sort($DirectoryFiles);
    # Get the correct the file path by comparing the directory with 
the variable $PHP_SELF if $LinkTo is not set
    if (!$LinkTo) {
      $LinkTo = $HTTP_HOST . substr($Directory, strpos($Directory, 
substr($PHP_SELF, 0, strrpos($PHP_SELF, "/"))), strlen($Directory));
    }
    # Display all the files and links to them.
    if (count($DirectoryFiles) > 0) echo "<ul>\n";
    for ($idx=0; $idx < count($DirectoryFiles); $idx++) {
      printf ("<li><a href=\"%s%s\">%s</a> <small>[%sk]
</small></li>\n", $LinkTo, $DirectoryFiles[$idx], $DirectoryFiles
[$idx], round(filesize($Directory.$DirectoryFiles[$idx])/1000,0));
    }
    if (count($DirectoryFiles) > 0) echo "</ul>\n";
    # Close directory
    closedir($DirectoryHandle);
  }
  ListFiles($TheFileType, $TheDirectory, $LinkTo);
?>