Jump to content

[SOLVED] Including .txt files with html content


mmitdnn

Recommended Posts

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? ;D

 

Thanks,

 

George

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.