Automatically generate list of file titles
Moderator: General Moderators
-
wmjsimpson
- Forum Newbie
- Posts: 1
- Joined: Mon Oct 12, 2009 9:42 am
Automatically generate list of 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 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>
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>
Re: Automatically generate list of file titles
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.
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.
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.
Re: Automatically generate list of file titles
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..
This:
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.
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>Code: Select all
($dir.$file)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
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
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
-
jacobian64
- Forum Newbie
- Posts: 4
- Joined: Tue Mar 02, 2010 11:43 pm
Re: Automatically generate list of file titles
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
Re: Automatically generate list of file titles
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.
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
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?Kurby wrote: You don't want a PHP file running constantly or rely on somebodies browser refreshing it.
Re: Automatically generate list of file titles
Exactly: "refresh". Not "keep running in the background, constantly checking if you've made any changes".jacobian64 wrote:I need the php code to refresh itself periodically
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
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.
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.