Automatically create hyperlinks of all files in a folder

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).

Who is Participating?
Eddie ShipmanAll-around developerCommented:
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>";
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();
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:
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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

See example here:

tom12gaAuthor Commented:
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.
Eddie ShipmanAll-around developerCommented:
Did you see this comment?
"// change the path_to_pdf_dir to your directory..."
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.