Darkelve Posted February 15, 2007 Report Share Posted February 15, 2007 Does anyone know how I can write a script that: - reads in all the files in a particular directory - displays the file names in a html list and makes a link of them: <ul> <li><a href="filelocation1">filename 1</li> <li><a href="filelocation2">filename 1</li> <li><a href="filelocation3">filename 1</li> </ul> etc. ? So basically it creates a list of links with the contents in that directory, so you can download them from there. Quote Link to comment Share on other sites More sharing options...
neddie Posted February 15, 2007 Report Share Posted February 15, 2007 Should already be built-in to the web server (if it's enabled). For example, see this: http://anorien.csc.warwick.ac.uk/mirrors/M...ial/iso/2007.0/ - That's not a php script, that's just the web browser pointed at the directory. Quote Link to comment Share on other sites More sharing options...
wingcom Posted February 15, 2007 Report Share Posted February 15, 2007 <?php $dir = '/tmp'; $files1 = scandir($dir); ?> then just loop through the array and print them. for ($i = 0; $i <= count($files1); $i++) { //print your files here with html markup so you can link echo "<a href=\"".$dir."\/".$files1[$i]."\">".$dir."\/".$files1[$i]."</a>"; } top of my head, you might want to check the escaping chars and so on in the echo function cus i dont have a good editor in this little box that corrects me ;) You also have to put the other html tags on there :) Quote Link to comment Share on other sites More sharing options...
Mhn Posted February 15, 2007 Report Share Posted February 15, 2007 If you use apache: http://httpd.apache.org/docs/1.3/mod/mod_autoindex.html just add <Directory /web/docs> Options Indexes </Directory> in httpd.conf (replace /web/docs with the correct directory) Quote Link to comment Share on other sites More sharing options...
Darkelve Posted February 15, 2007 Author Report Share Posted February 15, 2007 (edited) No, actually it's IIS running PHP (I know, I know... :| this is for an MS-only shop...) Edited February 15, 2007 by Darkelve Quote Link to comment Share on other sites More sharing options...
Mhn Posted February 15, 2007 Report Share Posted February 15, 2007 MS-only with PHP? :huh: IIS should have something similar like apache I think... but don't know Quote Link to comment Share on other sites More sharing options...
Darkelve Posted February 15, 2007 Author Report Share Posted February 15, 2007 Yeah, actually usually I use ASP, but PHP runs on it too and I'm trying to learn PHP so it's good practice to use that. MS-only with PHP? :huh: IIS should have something similar like apache I think... but don't know Quote Link to comment Share on other sites More sharing options...
tyme Posted February 28, 2007 Report Share Posted February 28, 2007 it'd be better practice to run php on apache with mysql ;) but seriously...wingcom's suggestion earlier should work. i do something similar, though it's more specific to reading jpg files and displaying them: $files = array(); $dir = opendir('./Pictures/'); while(($file = readdir($dir)) !== false) { if (!is_dir("$directorypath/$file") && (strstr("$file",".jpg") || strstr("$file",".JPG"))) { $files[] = $file; } } closedir($dir); for($i=0; $i<count($files); $i++) { echo "<a href=\"$address/browse.php/$i\" class=\"thumbnail\">"; echo "<img src=\"$address/Pictures/thumbs/$files[$i]\">"; echo "</a>"; } I do it this way, with the while loop, so that I can check and make sure the file meets certain specs (it's a jpeg and it's not a directory). This probably isn't the best example ever, as I hacked it together late one night. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.