Solved

Display Folder Contents on Page, PHP opendir

Posted on 2007-03-29
9
1,116 Views
Last Modified: 2008-02-07
I'm trying to display a directory of PDF files on my website.  The following script is yielding a blank page:

<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
echo "<h1>MSDS Docs</h1>";

$path = "/v2/docs/";

$dh = opendir($path);
while (($file = readdir($dh)) !== false) {
    echo "<a href='$path/$file'>$file</a><br />";
}
closedir($dh);

?>

What am I missing here?  To what extent will I be able to format the output, if I ever get there...........
0
Comment
Question by:pmagony
  • 6
  • 3
9 Comments
 
LVL 28

Expert Comment

by:gamebits
ID: 18815948
Make sure your path is correct could be ../v2/docs/
0
 
LVL 9

Author Comment

by:pmagony
ID: 18816147
The file that contains this script, is at the root of the web directory (httpdocs).  The subfolder /docs/ contains the PDF's I am referring too...  

So I've tried:

/docs/
/docs
docs

and nothing shows on the page.
0
 
LVL 9

Author Comment

by:pmagony
ID: 18816570
Hi,

Ok, well atleast now I can get the page to tell me that it's unable to open the path I am specifying.  I've pointed all the way from server root to the main directory itself, and it produces the same error for all paths.

Do you think this is a permission error?
0
 
LVL 9

Author Comment

by:pmagony
ID: 18816573
This would  help:

<?

//define the path as relative
$path = "manuals";

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

echo "Directory Listing of $path<br/>";

//running the while loop
while ($file = readdir($dir_handle))
{
   if($file!="." && $file!="..")
      echo "<a href='$file'>$file</a><br/>";
}

//closing the directory
closedir($dir_handle);

?>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Author Comment

by:pmagony
ID: 18816636
I got it... it's working!   For the others who read this, the path has to be the full server path.  Relative paths will not work.
0
 
LVL 28

Accepted Solution

by:
gamebits earned 500 total points
ID: 18816769
I told you to make sure the path was correct ;-)
0
 
LVL 9

Author Comment

by:pmagony
ID: 18816972
Just like my wife :^P
0
 
LVL 28

Expert Comment

by:gamebits
ID: 18817018
Oh, you got one like that too, thanks for the points and the grade.
0
 
LVL 9

Author Comment

by:pmagony
ID: 18817107
Haha... no problem... thanks for the assist.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

920 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

17 Experts available now in Live!

Get 1:1 Help Now