?
Solved

Automatic file listing in HTML or similar

Posted on 2006-05-01
9
Medium Priority
?
350 Views
Last Modified: 2010-04-06
Hi everyone

I'd like to generate an HTML page that lists all the files contained within a specific directory on our web server.  The server is running the usual services so it doesn't matter if the solution is written in HTML, Perl, PHP, whatever - as long as it's simple.

So, for example, when a visiter opens the page www.sitename.com/filelist.html, the page contains a list of all the files in www.sitename.com/files

I'd then like to have an image or button next to each item called 'Download' that visitors click to retrieve the appropriate file.

The next step is to provide another page that allows visitors to upload but that solution seems more common, there appear to be a few examples around so I'll try those!  I'll also need to add security at some point, so users have to log in before they can upload or download - but that can wait.

Thanks


Simon
0
Comment
Question by:SimonUK
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 11

Expert Comment

by:walkerke
ID: 16576810
What type of server do you have ... Windows, Unix, MacOS?
0
 
LVL 8

Expert Comment

by:KennyTM
ID: 16577296
Hi.

(For PHP)

Use opendir()... readdir()... etc.
Or simply use glob().
0
 
LVL 14

Assisted Solution

by:nltech
nltech earned 400 total points
ID: 16578224
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:SimonUK
ID: 16578401
Thanks so far.

It's a Linux server - a fairly standard hosting package.

Great suggestion on the software packages, but they're all fairly extensive and do more than I'm going to use.  What I'm looking for is a page of HTML or PHP that will generate a list of files that exist in a given location, that people can then download - it would be possible to create the 'add file' part and the security later - it's just a proof-of-concept at this stage.

By the way, I'm not familiar with the PHP language, KennyTM - so I'll need some code to get started with !

Thanks


Simon
0
 
LVL 8

Assisted Solution

by:KennyTM
KennyTM earned 400 total points
ID: 16578563
Basically...

<?php

function make_row ($s) {
  return "<tr><td>$s</td><td><a href='files/$s'>Download</a></td></tr>\n";
}

$files = glob('*');
// Fetch all file names

$files = array_map("make_row", $files);
// Format the file names for output

$str = implode("", $files);
// Join the array to make it a string

$str = "<table border='1' bordercolor='white'><col style='background-color: #CCCCFF;'></col><col style='background-color: #9999FF;'></col>$str</table>";
// Format it further

echo $str;
// Print the string.

?>
0
 
LVL 14

Expert Comment

by:nltech
ID: 16580152
i was thinking ahead to where you'll have these file transfers behind a user login. you'll need something more robust than a simple download or upload script, and that's where any of those three scripts will come in to play.  and if you aren't a coder, you'll be best off with a ready-made script. sure it might take a little while to get set up (i use two of them on my own servers, they really aren't too bad to install), but your time invested in them will not be wasted.
0
 
LVL 2

Accepted Solution

by:
Mr_Lenehan earned 1200 total points
ID: 16581489
In php what you need to do is inset this code to build a simple list of files in a directory:
<?php
$dirpath = "uploads";
    $dh = opendir($dirpath);
       while (false !== ($file = readdir($dh))) {
            //Don't list subdirectories
          if (!is_dir("$dirpath/$file")) {
           //Truncate the file extension and capitalize the first letter
           echo "filename: " . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . '.<br>';
   }
}
closedir($dh);
?>

What this will do is just print the filename with no file extention.  You'll need to change $dirpath to equal the directory you want the files to be listed from.

If you want to print the whole filename including extention replace:
    echo "filename: " . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . "<br>";
with the simpler:
    echo "filename: " . $file . "<br>";

You can also edit whatever is in the " " to make your html output better looking.  One example would be a drop down box in a select statement like this:

<?php
echo "<form>";
    echo "<select name='yourfiles'>";
    $dirpath = "/path/to/your/directory";
    $dh = opendir($dirpath);
       while (false !== ($file = readdir($dh))) {
          if (!is_dir("$dirpath/$file")) {
           echo "<option value='$file'>" . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . '</option>';
          }
       }
     closedir($dh);
echo "</select>";
echo "</form>";
?>

Hope this is what you're looking for.


0
 
LVL 2

Expert Comment

by:Mr_Lenehan
ID: 16581525
Oh, to make the files downloadable with a little image you could do:
<?php
$imageLocation = "images/myImage.jpg"
$dirpath = "somedirectory";
    $dh = opendir($dirpath);
       while (false !== ($file = readdir($dh))) {
          if (!is_dir("$dirpath/$file")) {
               echo htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . "<a href='$dirpath/$file'><img src='". $imageLocation."'></a><br>";
          }
       }
closedir($dh);
?>

This would produce output like: somefile [IMAGE] where you could click on the image to download the file.  Does that look like what you are looking for?
0
 
LVL 1

Author Comment

by:SimonUK
ID: 16678702
Thanks everyone.

I hope I've divided the points fairly.  Mr Lenehan came up with the specific answer I wanted but KennyTM also provided code that might be helpful to others, and nltech's links were VERY handy and I'm testing one of the products now.


Simon
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

840 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