Automatically create hyperlinks of all files in a folder

Posted on 2006-04-26
Last Modified: 2008-02-01
I have a lot of PDF’s and I want to automatically link to them from a webpage. Does anyone know a quick way to automatically create hyperlinks on a webpage from the folder (full of PDF’s).

Question by:tom12ga
    LVL 26

    Accepted Solution

    You have to create a server-side script to do that.
    This is how to do it using PHP:

    // change the path_to_pdf_dir to your directory...
    if ($handle = opendir('path_to_pdf_dir')) {
      echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">';
      echo "<html>";
      echo "  <head>";
      echo "    <title>PDF List</title>";
      echo "  </head>";
      echo "  <body>";
      /* This is the correct way to loop over the directory. */
      while (false !== ($file = readdir($handle))) {
          if ($file != '.' && $file != '..' ) {
          echo "<a href=\"".$file."\">".$file."</a><br />";
    echo "  </body>";
    echo "</html>";
    LVL 7

    Expert Comment

    If you are looking for a Javascript solution you can try to use JS ActiveX extensions

    document.write("Contents of " + getCurrentFolder());

    fileName = findFirstFile("*.pdf");
        document.write("<a href=" + fileName + ">" + fileName + "</a>");
        fileName = findNextFile();
    LVL 12

    Expert Comment

    This is how you do it in ASP. Of course you change the path in the fs.GetFolder to reflect the folder you want to use:

    --- Example start ---
    <html><head><title>PDF List</title></head>
    dim fs,fo,x
    set fs=Server.CreateObject("Scripting.FileSystemObject")
    set fo=fs.GetFolder("c:\")

    for each x in fo.files
      'Print the name and size of all files in the test folder
      Response.write(x.Name & " (" & CLng(x.Size / 1024) & "kb) <br />")

    set fo=nothing
    set fs=nothing
    --- Example end  ---

    You can also see this question for some more information:
    LVL 32

    Expert Comment

    Alternatively, allow directory browsing in IIS or turn Indexing on in Apache.
    This will automatically display a list of documents.

    See example here:


    Author Comment

    Thanks for all the info. The PHP code works pretty well but it was looking in the root for the links not the folder. Easy fix.
    LVL 26

    Expert Comment

    Did you see this comment?
    "// change the path_to_pdf_dir to your directory..."

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now