kristen252 Posted March 17, 2009 Share Posted March 17, 2009 I am very new to php, but I am trying to use the following code to import data from a txt file that is saved on my server: <?php $filename="totals.txt"; $handle=fopen($filename,"r"); $contents=fread ($handle,filesize($filename)); echo($contents); ?> The problem is that code imports the whole file- is there any way to import only part of the file? Say the last part of the first line? The txt file is generated by a form on my website and the form is automatically placing the persons ip address and I don't want that to show on our site. Or maybe I'm just going about this the wrong way and there is a form you can use in php to allow the visitor to your site to post to a certain page on your site? Any help or direction you could provide would be appreciated! As I mentioned, I'm very new at this, but I would like to learn! Quote Link to comment https://forums.phpfreaks.com/topic/149760-importing-from-a-txt-file/ Share on other sites More sharing options...
.josh Posted March 17, 2009 Share Posted March 17, 2009 whole file is being retrieved because you are using fread, which returns x bytes length of data, and you are requesting info of filesize($filename) length (the whole file). show an example of the file contents, and what part you want retrieved from it. Quote Link to comment https://forums.phpfreaks.com/topic/149760-importing-from-a-txt-file/#findComment-786424 Share on other sites More sharing options...
scarhand Posted March 17, 2009 Share Posted March 17, 2009 you need to explode the file contents into an array by using "\n" (which means linebreak) as a delimiter that way you can work with the array instead of the entire file as a string Quote Link to comment https://forums.phpfreaks.com/topic/149760-importing-from-a-txt-file/#findComment-786494 Share on other sites More sharing options...
kristen252 Posted March 17, 2009 Author Share Posted March 17, 2009 This is the text in the file: |2009-03-16 14:46:20|208.101.115.25|One-time: 50|Monthly: 500| I only want to import from "One-time" to the end of "500". Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/149760-importing-from-a-txt-file/#findComment-786765 Share on other sites More sharing options...
jlhaslip Posted March 17, 2009 Share Posted March 17, 2009 Read the line, explode the line on the pipe (|), but only handle (print) the last 2 elements of the line. Quote Link to comment https://forums.phpfreaks.com/topic/149760-importing-from-a-txt-file/#findComment-786781 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.