Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.