Solved

Display Folder Contents on Page, PHP opendir

Posted on 2007-03-29
9
1,123 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
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…

691 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