Show folder's contents as a file list in browser

They have: 16 posts

Joined: Aug 2007

I have a folder full of files (PowerPoint) on my webserver, and I want a web page to show a list of those files as download links. Also I need it to automatically update as and when I change the folder's contents by adding, deleting or renaming files. FTP access via the browser is not suitable in this case.

Is there a SIMPLE scripting/database solution? I don't want a sophisticated album, just a constantly-self-updating list of links.

Thanks guys!

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

This code will get the PowerPoint files into a PHP array:

<?php

/* change the directory to the location with the PowerPoint files */
$files = scandir('powerpoints');

foreach (
$files as $field => $value)
{
    if (
strtoupper(substr($value, -3, 3)) != 'PPT')
    {
        unset(
$files[$field]);
    }
}
sort($files);

?>

And then to loop through the array, something like this would work.

<?php foreach ($files as $field => $file): ?>

<p><a href="powerpoints/<?=$file?>"><?=$file?></a></p>

<?php endforeach; ?>

They have: 16 posts

Joined: Aug 2007

Cheers, I'll give it a try.

Dave.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

One note I would add would be to change the check for the file extension:

$aryExt = array('PPT','PPS'); // Place towards top of file

$strExt = strtoupper(substr($value,strrpos($value,'.')+1));
if (in_array($aryExt,$strExt) || $value==$_SERVER['SCRIPT_FILENAME']) {
   unset($files[$field]);
}

This accomplishes three things:

  1. Prevents a file like "testppt" (no extension) from matching.
  2. Makes it easy to take this code and use it in other directories for other file types. As the note says, put the first part towards the top of the file for easier finding and/or changing.
  3. If you were to use this to list out .php files, the code will also prevent the listing of itself. (generally, something like this would just be index.php, but I had it use the actual script name in case you save it as something else

-Greg

They have: 16 posts

Joined: Aug 2007

Thanks Greg, but can I ask you to put the whole script together? I'm not into writing them - just using them - and that very rarely.

Thanks,

Dave.

They have: 16 posts

Joined: Aug 2007

Sorry guys, but not being too familiar with PHP I'm lost with this.

I need to know exactly what to do here - I'm OK saving the file as a .php, but Greg's variation has confused me. I tried the original script and got an error, so I need a complete script, I need to know exactly what to edit to make it detect the files, and I need to know where is the best place to upload it.

Thanks!

Dave.

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

I tried the original script and got an error

Can you post the error?

They have: 121 posts

Joined: Dec 2008

Using Apache and lazy like me?

In the config, 'AllowIndexes' on the directory you are storing your PPT files in.
Apache will just show a directory listing of the files in the dir to the http client

No scripting necessary. If you aren't using Apache, I'm sure whatever other web server you are using has the equivalent.

Cheers,
Shaggy

They have: 16 posts

Joined: Aug 2007

Thanks Shaggy, but unfortunately my hosting company says it does not allow indexing on its servers. That's one reason for this request. Currently I have provided an FTP link to the folder, but it's very slow to show up - presumably due to icon drawing by browsers, and is not therefore very satisfactory.

I'll post an error log for pr0gr4mm3r soon.

Dave.

They have: 16 posts

Joined: Aug 2007

Here's the Error Log:

Warning: scandir(files) [function.scandir]: failed to open dir: No error in \\nas24ent\domains\h\mydomain.org\user\htdocs\files\myarray.php on line 4

Warning: scandir() [function.scandir]: (errno 0): No error in \\nas24ent\domains\h\mydomain.org\user\htdocs\files\myarray.php on line 4

Warning: Invalid argument supplied for foreach() in \\nas24ent\domains\h\mydomain.org\user\htdocs\files\myarray.php on line 7

Warning: sort() expects parameter 1 to be array, boolean given in \\nas24ent\domains\h\mydomain.org\user\htdocs\files\myarray.php on line 14

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Try running the script on a local filesystem and see if it helps.

greg's picture

He has: 1,581 posts

Joined: Nov 2005

Are you pointing to the correct DIR?

What exactly did you put as per pr0gr4mm3rs code:

<?php
/* change the directory to the location with the PowerPoint files */
$files = scandir('powerpoints');
?>

And what is the DIR (folder) where the powerpoint files are?

They have: 8 posts

Joined: Feb 2009

if u wud use perl...

it wud be..

print "system(ls -l)";

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.