I have a large number of simple html files. I was trying to figure out how I can use php to have a list of these files on a page, and when one is clicked on, it opens the file up in my 'template'. Instead of creating a full page for each file, it sounds like PHP would do the job nicely.
The problem is I cannot find any tutorials anywhere that cover something this simple with PHP. I just keep finding articles on writing your own functions, or basic tutorials that show how to put the current date/time on the page etc..
Anyone have any suggestions or know of a good site I could check out? Thanks.
Will PHP work for me?
Moderator: General Moderators
Hey, shouldn't be a problem!
To get a list of the files, you should have php iterate through your directory like so:
Ok, that's the list sorted, now you want links. When the script is displaying the filename, make it produce a link in the form script.php?file_to_include=blah.html
eg, change echo nl2br ( $file . "\n" );
to
in yourscript.php, have a section which includes the file:
So anyway, hope this helps, I havn't described it very well, and the code is just throne together untested, so no guarentee! Also, make sure to test for any security holes, eg making sure that directories above your website cannot be included if the user changes the website url. Possibly you could ensure that the referer to the page was always your listing page, that could help.
David
To get a list of the files, you should have php iterate through your directory like so:
Code: Select all
<?php
$dir = "what/ever/you/want/";
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo nl2br ( $file . "\n" );
}
}
closedir($handle);
}
?>eg, change echo nl2br ( $file . "\n" );
to
Code: Select all
<?php
echo nl2br ( "<a href='yourscript.php?file_to_include=" . $file . "' >" . $file . "\n" );
?>Code: Select all
<?php
if ( isset ( $_GET["file_to_include"] ) {
include ( $_GET["file_to_include"] ); // You probably should make sure the file exists first before including it, also need to check that the file is allowed to be included, so as to prevent a security risk
} else {
echo "No file to include";
}
?>David
Thanks for the info.. I don't know anything about PHP yet so I don't know if I should dive into it quite yet.. I had not even thought about security etc.. I am thinking of just using something like PHPNuke for what I need.. I will sit down and learn to use PHP 'manually' eventually.. I just have a lot of things i'm learning currently as it is 