mmitdnn Posted May 5, 2007 Share Posted May 5, 2007 Hi guys, I have a php page and I want to include some txt files within it depending on the URL variable. For example, I have some text files (that contain html code) with these names: cat.txt dog.txt parrot.txt How can I make the contents of, say, "parrot.txt" appear on the page, if I use a URL like: mysite.com/page.php?c=parrot "c" stands for "content" and each time I want to include a file, I would use the exact name of the txt file - but without the .txt extention. I know this is possible and I had the code to do it but it's been over two years and I have misplaced the code. Any ideas how it's done? Thanks, George Link to comment https://forums.phpfreaks.com/topic/50105-solved-including-txt-files-with-html-content/ Share on other sites More sharing options...
Lumio Posted May 5, 2007 Share Posted May 5, 2007 <?php $filename = ''; $c = ''; if (isset($_GET['c'])) $c = $_GET['c']; if ($c == 'parrot') $filename = 'parrot.txt'; elseif ($c == 'dog') $filename = 'dog.txt'; //and so on... $filesize = filesize($filename); $content = ''; if ($filesize > 0) { $fp = fopen($filename, 'r'); $content = fread($fp, $filesize); fclose($fp); } echo $content; ?> Link to comment https://forums.phpfreaks.com/topic/50105-solved-including-txt-files-with-html-content/#findComment-246030 Share on other sites More sharing options...
mmitdnn Posted May 5, 2007 Author Share Posted May 5, 2007 Worked like a charm. Thanks Lumio - I appreciate it... Link to comment https://forums.phpfreaks.com/topic/50105-solved-including-txt-files-with-html-content/#findComment-246280 Share on other sites More sharing options...
triphis Posted May 5, 2007 Share Posted May 5, 2007 If you are going to have a TONNE of different animals (or whatever) you can use: $filename = ''; $c = ''; if(isset($_GET['c'])) { $c = $_GET['c']; $filename = $c . ".txt"; } because it is unreasonable to have a hundred different if statements. I didn't show the complete code... hopefully you can adapt if needed. Link to comment https://forums.phpfreaks.com/topic/50105-solved-including-txt-files-with-html-content/#findComment-246287 Share on other sites More sharing options...
Lumio Posted May 6, 2007 Share Posted May 6, 2007 and what if $_GET['c'] = '../log' ? So it's more secure to see, what's in $_GET['c'] Link to comment https://forums.phpfreaks.com/topic/50105-solved-including-txt-files-with-html-content/#findComment-246545 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.