Solved

Display Folder Contents on Page, PHP opendir

Posted on 2007-03-29
9
1,120 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hosting images 4 29
write screen output to text file 21 47
000webhost.com default error log 1 28
PHP Installer 5 24
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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.

772 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