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;?> Quote Link to comment 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>'; Quote Link to comment 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;?> Quote Link to comment 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>'; ?> Quote Link to comment 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? Quote Link to comment 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? Quote Link to comment 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 ^^ Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 ? Quote Link to comment Share on other sites More sharing options...
Rifts Posted September 28, 2010 Share Posted September 28, 2010 install this http://www.wampserver.com/en/ Quote Link to comment 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 ' '; ?> Quote Link to comment 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.