I am trying to make a website that uses fopen to open up text file. Something like this http://somesite.com/readarticle.php?name=sample.txt

<?php
$file = 'sample.txt';
$f = fopen($file, r);
while ( $line = fgets($f, 1000) ) {
print $line;
}
?>

The sample.txt is located on the web server, I was able to make this work but my problem now is how do I use the same php file on opening other text file without specifying filename on the php file itself?

Something like this..

http://somesite.com/readarticle.php?name=sample.txt
http://somesite.com/readarticle.php?name=sample2.txt
http://somesite.com/readarticle.php?name=sample3.txt

Sorry for my bad english..:)

Recommended Answers

All 3 Replies

You can get URL value's with $_GET[] array, in your sample URL (
http://somesite.com/readarticle.php?name=sample.txt) it would be $_GET['name']; . Be warned to use this cause this brings potential security leeks with it. you should read up on the problems with it.

<?php
$file = $_GET['name'];
$f = fopen($file, r);
while ( $line = fgets($f, 1000) ) {
print $line;
}
?>

now if I go to readarticle.php?name=readarticle.php I can see the PHP source.
Good luck with your site.

Oh? Thank you! Is there any way I could use this function without this information leak? Any suggestions?

yeah.. but, use if conditional to safely your site. like this
if ($_GET == $_PHPSELF) die(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.