Listing Files in a Directory

Posted on 2007-04-11
Last Modified: 2008-02-01
I'm trying to set up part of my website so it's easier to share files. To be frank I have no clue where to search.
There are a few things I would like it to do if any one can help:
- List files in the directory so that I don't need to update the page (auto list )
- Not have the exact location but have them as lets say" file.php?filename=test.rar" (stops leaching)

Formatting would be the easy part (i think) because that could be done in a loop if I'm correct

Please if anyone could give be a shove or a kick in the right direction I would be great full.
Question by:peterallsop
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
  • 3
  • 2

Expert Comment

by:Zack Soderquist
ID: 18892721
You may want to look at some of the built in PHP Directory Functions

scandir might be a good one

Here's a link to all the directory functions

Accepted Solution

netmunky earned 500 total points
ID: 18892725
has documentation on most of the functions you need to get the list

for getting the file, you'll need

you'll needs some security checks in this (so you don't serve /etc/passwd), but the gist of it is something like:
$finfo = finfo_open();
header( "Content-type: ".finfo_file( $finfo, $_GET['filename'] ) );
readfile( $_GET['filename'] );

Expert Comment

ID: 18892737
if you don't have access to install the PECL finfo extension, check out the depricated
Technology Partners: 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!


Expert Comment

by:Zack Soderquist
ID: 18892775
scandir only works with PHP5

Here is a PHP4 alternative

$dir = "/tmp";
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
    $files[] = $filename;

Expert Comment

by:Zack Soderquist
ID: 18892806
$dir = "/tmp";
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
    $files[] = $filename;

foreach($files as $value) {
    echo "file.php?filename=".$value."<br />";

Author Comment

ID: 18904446
I'm using PHP 5 just to let you know. Thanks for all the info.

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP 5.6 and 7.x 4 40
Using Modal's in to Retrieve Data from MySql and Populate Forms 13 87
Make check boxes work 8 46
Why is my $_POST not going to results page 10 41
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
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.

726 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