Powered by:
FutureQuest Hosting


"...Remember being a youngster, mom or dad telling you "close the door"? Well the same applies to HTML ...."

Web Hosting
Shared       
Reseller     
VPS             
Dedicated 
Price:   $(US)
Space:     (MB)
Transfer:(GB)
Platform:


Advertise
Advertising Opportunities
Rate Card

Sponsor
Go Back   Webmaster Forums > Code Forum > PHP Forum


Reply
 
Thread Tools Rate this Webmaster Discussion
  #1  
Old 10-09-2009, 06:06 PM
wmjsimpson wmjsimpson is offline
Junior Member
 
Join Date: Oct 2009
Webmaster Discussions: 1
Rep Power: 1
wmjsimpson is on a distinguished road
PHP for automatically generating file/link list with file titles

I keep a website for all my classes, and one of the pages is a Downloads page where students can get all the documents I pass out in class. In the past, I manually added to the list in a normal HTML document, but I'd like to switch to PHP so the list is generated automatically. I tried the script below and it works just fine, but it generates a list of file names. Is it possible to have this (or another code) generate the actual file titles? For example, it'd be easier for my students to see Chapter 1 Vocabulary rather than av1_ch1_vocab.pdf.

I'm fairly clueless when it comes to PHP, but I can copy and paste and work with typical HTML. I'd really appreciate your help in shaving off a few minutes of website updating every day!

The current PHP I'm using is:


<ul>
<?

// Define the full path to your folder from root
$path = "/rhsfrench/downloads/";

// Open the folder
$dir_handle = @opendir($path) or die("Unable to open $path");

// Loop through the files
while ($file = readdir($dir_handle)) {

if($file == "." || $file == ".." || $file == "index.php" )

continue;
echo "<li><a href=\"$file\">$file</a></li>";

}

// Close
closedir($dir_handle);

?>
</ul>


Reply With Quote
  #2  
Old 10-13-2009, 11:56 AM
jthayne's Avatar
jthayne jthayne is offline
Senior Member
 
Join Date: Aug 2008
Location: Texas
Webmaster Discussions: 487
Rep Power: 15
jthayne is the hardest working person in the biz. jthayne is the hardest working person in the biz. jthayne is the hardest working person in the biz. jthayne is the hardest working person in the biz.
Re: PHP for automatically generating file/link list with file titles

If you are consistent in your file names, you can create the display name based on the actual file name.

For example:

File Name: av1_ch1_vocab.pdf

PHP code to get the Display Name of each file:
PHP Code:
// Loop through the files
while ($file readdir($dir_handle)) {
    if(
$file == "." || $file == ".." || $file == "index.php" ) {
        continue;
    }

    
$fn explode("_"$file);

    
$fn[1] = substr($fn[1], 2);
    
$name "Chapter " $fn[1];

    switch (
$fn[2]) {
    case 
'vocab':               /* <---- Add a new case for each type of file */
        
$name .= " Vocabulary";
        break;
    default:                    
/* <---- Place the default name here for those file you forgot to add a case statement for or just don't fit. */
        
$name .= " Summary";
    }

    echo 
"<li><a href='".$file."'>".$name."</a></li>";

Let me know if you have any further questions about it.
__________________
Be sure to click the reputation icon to give rep to the person who helped you.
Check out the following sites: www.inbutnotoftheworld.com and www.mythoughtexactly.com.
Reply With Quote
Reply
Sponsor

Bookmarks

Tags
file or link , php

Thread Tools
Rate This Thread
Rate This Thread:

 

Similar Webmaster Discussions
Thread Webmaster Discussion Starter Forum Replies Last Post
Need help creating a dvd inventory list from a CSV file aldar HTML Forum 5 08-22-2008 04:50 PM
How to print a html file to a specific printer automatically stoopenf Website Scripts Forum 0 08-10-2005 04:33 AM
how to make a css file to link to your webpages Main Source CSS Forum 1 01-15-2004 12:30 AM
How to Insert a Link That Will Download a File rowdyr HTML Forum 2 04-11-2003 09:51 AM
Help with file link switherow HTML Forum 7 07-31-2001 02:34 AM


All times are GMT -5. The time now is 04:47 PM.

Copyright © 1999 - 2009 Advanced HTML For Beginners and AHFB2000. All rights reserved.
Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.