Page 1 of 1

Will PHP work for me?

Posted: Wed Apr 07, 2004 5:43 pm
by Aleister
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.

Posted: Wed Apr 07, 2004 5:59 pm
by werlop
Hey, shouldn't be a problem!

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); 
}
?>
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

Code: Select all

<?php
echo nl2br ( "<a href='yourscript.php?file_to_include=" . $file . "' >" . $file . "\n" ); 

?>
in yourscript.php, have a section which includes the file:

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";

}
?>
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

Posted: Wed Apr 07, 2004 6:20 pm
by Aleister
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 :)

Posted: Wed Apr 07, 2004 6:21 pm
by markl999
I had not even thought about security etc.. I am thinking of just using something like PHPNuke
Sorry, can't resist ... but 'security' and 'phpNuke' shouldn't be used in the same sentence, it's security history isn't too great :o

Posted: Wed Apr 07, 2004 6:29 pm
by werlop
markl999 wrote: Sorry, can't resist ... but 'security' and 'phpNuke' shouldn't be used in the same sentence, it's security history isn't too great :o
Only in the same way that the wonderful thing called the oxymoron allows Microsoft and Works to be used in the same sentence. :)

Posted: Wed Apr 07, 2004 7:09 pm
by Aleister
Perhaps, but PHP Nuke is going to take a lot less of php coding for a lot more features. I have heard it is not the most secure either, but more secure than my current attempts would be :)

I do intend to learn it at some point as I mentioned, that day is just not today :)

Posted: Wed Apr 07, 2004 7:51 pm
by Aleister
Well looking over it again, a system like PHP Nuke is not going to work at all for the kind of site I have.. I am going back to my original plan of just using a bit of php for the lyrics thing...

Now I need to go about making it secure! :P