Click to See Complete Forum and Search --> : Create links on the fly
Mondai
03-20-2005, 03:14 PM
Is it possible to build a list of links out of files in a directory?
Example:
I have a directory website\pages\
I want to build a list of links based on *.html in that directory
Each link name would be the name of an html file.
Can anoyone show me some sample code to do this?
Thanks
NogDog
03-20-2005, 03:39 PM
You would need to use some sort of server-side programming, such as PHP or Perl CGI to do this. Which to use depends on what your web host provides and what you're comfortable working with.
Mondai
03-20-2005, 04:02 PM
yep I can use php on my server, just don't know how to write the code for it.
NogDog
03-20-2005, 04:38 PM
<?php
if ($handle = opendir('/path/to/files'))
{
echo "<ul>\n";
while (false !== ($file = readdir($handle))) {
if(preg_match("/.+\\.html$/", $file) == 1)
{
echo "<li><a href='/path/to/files/$file'>$file</a></li>\n";
}
}
echo "</ul>\n";
closedir($handle);
}
else
{
echo "<p>ERROR: Could not open directory.</p>\n";
}
?>
ray326
03-20-2005, 05:15 PM
Of course the server will do that for you automagically if indexing is allowed and you don't have an index.* file in that directory.
Mondai
03-20-2005, 07:05 PM
Thank you so much NoDog! If I wanted to show the files with out the extenion (.html) , how would I do that?
MstrBob
03-20-2005, 07:22 PM
<?php
$dir = "./";
if ($handle = opendir($dir))
{
echo "<ul>\n";
while (false !== ($file = readdir($handle))) {
if(preg_match("/.+\.html$/", $file) == 1)
{
$filename = preg_replace("/\.html$/", "", $file);
echo "<li><a href='$dir$file'>$filename</a></li>\n";
}
}
echo "</ul>\n";
closedir($handle);
}
else
{
echo "<p>ERROR: Could not open directory.</p>\n";
}
?>
NogDog
03-20-2005, 08:24 PM
Or...
<?php
if ($handle = opendir('/path/to/files'))
{
echo "<ul>\n";
while (false !== ($file = readdir($handle))) {
if(preg_match("/(.+)\\.html$/", $file, $matches) == 1)
{
echo "<li><a href='/path/to/files/$file'>{$matches[1]}</a></li>\n";
}
}
echo "</ul>\n";
closedir($handle);
}
else
{
echo "<p>ERROR: Could not open directory.</p>\n";
}
?
Mondai
03-24-2005, 06:21 PM
Thanks guys, it works like a champ! Couple more questions.
1. How can I display the number of files it found? Like %x% results found
2. How do I use parameter as a variable? Example, I want to call this php script from a link that will send the script a directory name to be used. I'll have like 10 links and this way I won't have to have 10 php scripts, i can just use one. is this possible?
MstrBob
03-24-2005, 09:07 PM
Erm, something like:
<?php
$count=0;
if ($handle = opendir($_GET['dir']))
{
echo "<ul>\n";
while (false !== ($file = readdir($handle))) {
if(preg_match("/(.+)\.html$/", $file, $matches) == 1)
{
$count++;
echo "<li><a href=\"{$_GET['dir']}\">{$matches[1]}</a></li>\n";
}
}
echo "</ul>\n";
echo("<p>$count files found.</p>");
closedir($handle);
}
else
{
echo "<p>ERROR: Could not open directory.</p>\n";
}
?>
When you save this as a file, you can call it by
filename.php?dir=/dir/to/check
Keep in mind though, that you may want to check that the directory passed is from an array, or do some other form of validation so you can control what directories are being read.
Mondai
03-24-2005, 09:59 PM
Sweet I got it to work. I am actually learning somthing. Many thanks! Does stuff like this hurt search engine results?
the tree
03-25-2005, 01:23 AM
Search engines wouldn't even notice that it had been done.