Automatically generate list of file titles

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wmjsimpson
Forum Newbie
Posts: 1
Joined: Mon Oct 12, 2009 9:42 am

Automatically generate list of file titles

Post by wmjsimpson »

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 inexperienced 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>
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Automatically generate list of file titles

Post by JNettles »

Wellll....... there's several ways you could do this. The easiest, of course, is to start naming your documents what you'd like to them to show up as. :wink:

A more elegant solution would be to develop an admin panel of sorts that allows you to control what files are visible, accessible, when they become available etc. and you would be able to assign a name to each file. This would require a bit of work, though it wouldn't be too terribly hard and I'm sure you could write in a couple hours time.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Automatically generate list of file titles

Post by Weiry »

Please wrap your php code using the tags... see my comment for more details.

But i checked out the example code at php.net and modified it slightly..

Code: Select all

<ul>
<?php
// Define the full path to your folder from root
$dir = "testdir/";
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if($file == "." || $file == ".." || $file == "index.php"){
                continue;
            }
            print "<li><a href='".($dir.$file)."'>{$file}</a></li>";
        }
        closedir($dh);
    }else{
        print "Error: Could not open directory {$dir}";
    }
}else{
    print "Error: '{$dir}' is not a directory";
}
?>
</ul>
This:

Code: Select all

($dir.$file)
is assuming that your running the script from the folder above where the files are located.
You could also be a little more creative and remove the file extensions if you really wanted to.
jacobian64
Forum Newbie
Posts: 4
Joined: Tue Mar 02, 2010 11:43 pm

Re: Automatically generate list of file titles

Post by jacobian64 »

I had execute the code above and found it to be useful in indexing a text file.

I am trying to make a software that can incrementally update it's indexes, and the above code can do just that. so I am just asking is there a way for the code above to be able to execute automatically during a period of times? so the code will check perodically if there are new files in the folder and if there are new files then the code will index and show it like the code above.

any help would greatly be appreciated. thanks
Kurby
Forum Commoner
Posts: 63
Joined: Tue Feb 23, 2010 10:51 am

Re: Automatically generate list of file titles

Post by Kurby »

Scheduled tasks can be performed with cron tab on a Unix system.

http://en.wikipedia.org/wiki/Cron
jacobian64
Forum Newbie
Posts: 4
Joined: Tue Mar 02, 2010 11:43 pm

Re: Automatically generate list of file titles

Post by jacobian64 »

I am using windows, I mean is there a function in php that can check the folder periodically. kind of like refresh. so I don't have to index it manually. thanks
Kurby
Forum Commoner
Posts: 63
Joined: Tue Feb 23, 2010 10:51 am

Re: Automatically generate list of file titles

Post by Kurby »

You can schedule tasks in windows too: http://www.google.com/search?rlz=1C1GGL ... in+windows

You don't want a PHP file running constantly or rely on somebodies browser refreshing it.
jacobian64
Forum Newbie
Posts: 4
Joined: Tue Mar 02, 2010 11:43 pm

Re: Automatically generate list of file titles

Post by jacobian64 »

Kurby wrote: You don't want a PHP file running constantly or rely on somebodies browser refreshing it.
why would I don't want it? actually that's my point. I need the php code to refresh itself periodically by putting some function in the code. isn't there any function in php that can do just that?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Automatically generate list of file titles

Post by requinix »

jacobian64 wrote:I need the php code to refresh itself periodically
Exactly: "refresh". Not "keep running in the background, constantly checking if you've made any changes".

There isn't a way to make a script run forever*. PHP does not run constantly. It's not meant to.

A TV is an analogy. You don't keep the TV on all the time - you turn it on when you want to watch it and off when you're done. It's plugged into the wall so you can use it at a moment's notice, drawing a little bit of power while waiting for you to turn it on.
PHP is the same: you don't run it all the time - just when you want it to do something. For webservers, PHP is sleeping quietly in the background, waiting for the server to tell it to act.



* Okay, I lie: it is possible, but we're not telling you how because you shouldn't. The correct solution is cron on Unix or the task scheduler on Windows.
You're a teacher, yes? No offense but in this case you're the student.
jacobian64
Forum Newbie
Posts: 4
Joined: Tue Mar 02, 2010 11:43 pm

Re: Automatically generate list of file titles

Post by jacobian64 »

allright, let me share you my problem then

I am doing a thesis and the title is

"incremental update of inverted lists for text document retrieval"

so now i am making the software, I had been able to make a software that can index the whole text file including all the words in it. but this software that I am building needs to be able to update itself incrementally based on the incoming file and only index the new file that arrives while leaving the old file unaffected. so whenever the code detects there is a new file in a folder then the code will index it and add it to the database (in this case it's mysql).

so what is the best way for me to be able to update the indexes in mysql then? is it better to use the function in php or use the task scheduler?

any help would be appreciated. thanks.
Post Reply