Show folder's contents as a file list in browser
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 posted this at 14:33 — 20th February 2009.
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; ?>
UK_Dave posted this at 14:55 — 20th February 2009.
They have: 16 posts
Joined: Aug 2007
Cheers, I'll give it a try.
Dave.
Greg K posted this at 15:25 — 20th February 2009.
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:
-Greg
UK_Dave posted this at 15:47 — 20th February 2009.
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.
UK_Dave posted this at 12:00 — 21st February 2009.
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 posted this at 15:36 — 21st February 2009.
He has: 1,502 posts
Joined: Sep 2006
Can you post the error?
Shaggy posted this at 04:56 — 22nd February 2009.
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
UK_Dave posted this at 10:36 — 23rd February 2009.
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.
UK_Dave posted this at 15:25 — 25th February 2009.
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 posted this at 17:49 — 25th February 2009.
He has: 1,502 posts
Joined: Sep 2006
Try running the script on a local filesystem and see if it helps.
greg posted this at 09:03 — 26th February 2009.
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?
neodeepak posted this at 14:12 — 27th February 2009.
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.