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

Go Back   Webmaster Forums > Code Forum > PHP Forum

Reply
 
Thread Tools Rate this Webmaster Discussion
Old 10-09-2009, 05:06 PM   #1
wmjsimpson
New Member
 
Join Date: Oct 2009
Webmaster Discussions: 1
Rep Power: 6
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>


wmjsimpson is offline   Reply With Quote
Old 10-13-2009, 10:56 AM   #2
jthayne
 
jthayne's Avatar
 
Join Date: Aug 2008
Location: Texas
Webmaster Discussions: 501
Rep Power: 21
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.
For web design/development services, check Silentium Designs.
jthayne is offline   Reply With Quote
Reply

Bookmarks

Tags
file or link, php

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


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 03:50 PM
How to print a html file to a specific printer automatically stoopenf Website Scripts Forum 0 08-10-2005 03: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 08:51 AM
Help with file link switherow HTML Forum 7 07-31-2001 01:34 AM


All times are GMT -5. The time now is 08:54 PM.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.