61 users online (1 members and 60 guests)  


  Results 1 to 2 of 2

Related

  1. Replies: 5
  2. need help with onclick link to external .JS file    Forum: Javascript Forum
    Replies: 0
  3. Replies: 0
  4. Replies: 1
  5. Help with file link    Forum: HTML Forum
    Replies: 7
  1. #1
    wmjsimpson's Avatar
    New User

    Status
    Offline
    Join Date
    Oct 2009
    Posts
    1

    PHP for automatically generating file/link list with file titles

    I keep a website for all my classes, and one of the pages is a Downloads page where students can get all the documents I pass out in class. In the past, I manually added to the list in a normal HTML document, but I'd like to switch to PHP so the list is generated automatically. I tried the script below and it works just fine, but it generates a list of file names. Is it possible to have this (or another code) generate the actual file titles? For example, it'd be easier for my students to see Chapter 1 Vocabulary rather than av1_ch1_vocab.pdf.

    I'm fairly clueless when it comes to PHP, but I can copy and paste and work with typical HTML. I'd really appreciate your help in shaving off a few minutes of website updating every day!

    The current PHP I'm using is:


    <ul>
    <?

    // Define the full path to your folder from root
    $path = "/rhsfrench/downloads/";

    // Open the folder
    $dir_handle = @opendir($path) or die("Unable to open $path");

    // Loop through the files
    while ($file = readdir($dir_handle)) {

    if($file == "." || $file == ".." || $file == "index.php" )

    continue;
    echo "<li><a href=\"$file\">$file</a></li>";

    }

    // Close
    closedir($dir_handle);

    ?>
    </ul>

  2. #2
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: PHP for automatically generating file/link list with file titles

    If you are consistent in your file names, you can create the display name based on the actual file name.

    For example:

    File Name: av1_ch1_vocab.pdf

    PHP code to get the Display Name of each file:
    PHP Code:
    // Loop through the files
    while ($file readdir($dir_handle)) {
        if(
    $file == "." || $file == ".." || $file == "index.php" ) {
            continue;
        }

        
    $fn explode("_"$file);

        
    $fn[1] = substr($fn[1], 2);
        
    $name "Chapter " $fn[1];

        switch (
    $fn[2]) {
        case 
    'vocab':               /* <---- Add a new case for each type of file */
            
    $name .= " Vocabulary";
            break;
        default:                    
    /* <---- Place the default name here for those file you forgot to add a case statement for or just don't fit. */
            
    $name .= " Summary";
        }

        echo 
    "<li><a href='".$file."'>".$name."</a></li>";

    Let me know if you have any further questions about it.



Tags for this Thread