nic james Posted September 28, 2010 Share Posted September 28, 2010 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 https://forums.phpfreaks.com/topic/214575-insert-text-into-the-body-content-from-a-txt-file/ Share on other sites More sharing options...
Pikachu2000 Posted September 28, 2010 Share Posted September 28, 2010 Since it's simply a text file, would this not work for you? echo '<pre>'; include('your_txt_file.txt'); echo '<pre>'; Link to comment https://forums.phpfreaks.com/topic/214575-insert-text-into-the-body-content-from-a-txt-file/#findComment-1116574 Share on other sites More sharing options...
joel24 Posted September 28, 2010 Share Posted September 28, 2010 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 https://forums.phpfreaks.com/topic/214575-insert-text-into-the-body-content-from-a-txt-file/#findComment-1116575 Share on other sites More sharing options...
nic james Posted September 28, 2010 Author Share Posted September 28, 2010 ok, i put this in and it still doesnt work, it seams to display the code itself and not the text file (sorry im new to php) <?php echo '<pre>'; include('indexbody.txt'); echo '<pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/214575-insert-text-into-the-body-content-from-a-txt-file/#findComment-1116584 Share on other sites More sharing options...
Pikachu2000 Posted September 28, 2010 Share Posted September 28, 2010 Are you testing this on a server? Are you sure PHP is enabled on the server? Link to comment https://forums.phpfreaks.com/topic/214575-insert-text-into-the-body-content-from-a-txt-file/#findComment-1116591 Share on other sites More sharing options...
joel24 Posted September 28, 2010 Share Posted September 28, 2010 and are you pointing to the txt file correctly, i.e. is the file in the same directory as the PHP script, or is it in a subfolder? Link to comment https://forums.phpfreaks.com/topic/214575-insert-text-into-the-body-content-from-a-txt-file/#findComment-1116627 Share on other sites More sharing options...
nic james Posted September 28, 2010 Author Share Posted September 28, 2010 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 https://forums.phpfreaks.com/topic/214575-insert-text-into-the-body-content-from-a-txt-file/#findComment-1116915 Share on other sites More sharing options...
BlueSkyIS Posted September 28, 2010 Share Posted September 28, 2010 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 https://forums.phpfreaks.com/topic/214575-insert-text-into-the-body-content-from-a-txt-file/#findComment-1116921 Share on other sites More sharing options...
Pikachu2000 Posted September 28, 2010 Share Posted September 28, 2010 You need to have some sort of webserver software such as Apache installed, as well as PHP for that to work. If your using WinD'ohs, look into installing WAMP. Link to comment https://forums.phpfreaks.com/topic/214575-insert-text-into-the-body-content-from-a-txt-file/#findComment-1116922 Share on other sites More sharing options...
nic james Posted September 28, 2010 Author Share Posted September 28, 2010 this one? http://www.wampserver.com/dl.php ? Link to comment https://forums.phpfreaks.com/topic/214575-insert-text-into-the-body-content-from-a-txt-file/#findComment-1116923 Share on other sites More sharing options...
Rifts Posted September 28, 2010 Share Posted September 28, 2010 install this http://www.wampserver.com/en/ Link to comment https://forums.phpfreaks.com/topic/214575-insert-text-into-the-body-content-from-a-txt-file/#findComment-1116930 Share on other sites More sharing options...
nic james Posted September 29, 2010 Author Share Posted September 29, 2010 it still doesnt seam to work... mmm :/ here is what its displaying: '; include('indexbody.txt'); echo ' '; ?> Link to comment https://forums.phpfreaks.com/topic/214575-insert-text-into-the-body-content-from-a-txt-file/#findComment-1116983 Share on other sites More sharing options...
Pikachu2000 Posted September 29, 2010 Share Posted September 29, 2010 How are you loading the file into the web browser, and what is the file name? Link to comment https://forums.phpfreaks.com/topic/214575-insert-text-into-the-body-content-from-a-txt-file/#findComment-1116985 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.