Jump to content

insert text into the body content from a .txt file


nic james

Recommended Posts

im working on my website and im trying to insert text from a file into the webpage.

 

the webpage is

index.php

 

the .txt file is

indexbody.txt

 

at the point of where the code is i would like to insert the contents of the text file into the html document so the person viewing the site can read it as if it was directly in that webpage.

i hope you understand what im asking

 

the current not working code is:

<?php $myFile = "indexbody.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, 5);
fclose($fh);
echo $theData;?>

you're only reading 5 bytes of the text file... read up on fread here

 

$theData = fread($fh, 5);
//the 5 indicates 5 bytes to be read, change this to filesize($myFile). i.e.

<?php $myFile = "indexbody.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;?>

the text file is in the same directory, what that code does is show the code itself and not the text. right now the website is offline in a folder on my hardrive. the index.php and indexbody.txt are in the same directory, im using firefox to open the index.php, and im designing the site with dreamweaver.

 

thanks

-nic ^^

you must have a web server parse the .php file and send it to the browser. simply opening a .php file using firefox will give you what you see. if you're on windows, I suggest that you look into wamp. for mac, I suggest mamp. although mac's come with PHP installed, i prefer to use the mamp package so i can screw that up instead of my mac's php installation.

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.