Jump to content

insert text into the body content from a .txt file


nic james
 Share

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ^^

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

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