Slips Posted December 30, 2010 Share Posted December 30, 2010 Hello all, I'm trying to open a file, take some text from it conditionally, and paste the newly edited text back into the same file. At first I used a seperate fopen($handle, 'r') handler to open the file to read it, get its contents CONDITIONALLY, close the read handler, and open a new write handler to overwrite the file. In short, I need to extract the text from the file after ignoring some amount of text , and then rewrite the file with the newly altered text. I was trying out r+ and w+ options when using fopen(), but w+ is working in a way thats leaving me confused. Here is my code. $file = 'test.txt'; $fileHandle = fopen($file,'w+'); fwrite($fileHandle, 'Newly inserted text'); $fileContents = fread($fileHandle, filesize($file)); echo $fileContents; fclose($fileHandle); The manual says that if the w+ option is specified then the file is opened for reading and writing. From what I understand this means that I should be able to read from the file after writing to it. But the code above displays nothing after writing to the file. The file is chmodded 777. Can someone please explain to me why this is so Thanks Quote Link to comment https://forums.phpfreaks.com/topic/222983-fopen-with-w-option-confuses-me/ Share on other sites More sharing options...
theverychap Posted December 30, 2010 Share Posted December 30, 2010 You would be a lot quicker using: file_get_contents() to echo the contents of $file: echo file_get_contents($file); Quote Link to comment https://forums.phpfreaks.com/topic/222983-fopen-with-w-option-confuses-me/#findComment-1152912 Share on other sites More sharing options...
revraz Posted December 30, 2010 Share Posted December 30, 2010 I believe you actually have to close the handle before it's actually written. Quote Link to comment https://forums.phpfreaks.com/topic/222983-fopen-with-w-option-confuses-me/#findComment-1152913 Share on other sites More sharing options...
Slips Posted December 30, 2010 Author Share Posted December 30, 2010 Ok firstly I cannot use file_get_contents, because I want to read the file line by line and omit the lines I don't want. Using file_get_contents() with a regex to sniff those lines out seems more complex. Also file_get_contents is not preserving line breaks, something which I'll have to add manually. Basically what I'm trying to do, is remove entries for a web project on prompt from the /etc/hosts file. I was using file handlers because I can scan the file line by line and dump only the necessary entries into a new variable and omit the unwanted entries. This 'new' config will then overwrite the previous hosts file. Here is what my code must do : Create new variable that will hold the new overwriting config Open hosts file Scan entires in it line by line and dump lines that are needed into the variable we created Overwrite the old config text with the new config txt in the variable with fwrite() Quote Link to comment https://forums.phpfreaks.com/topic/222983-fopen-with-w-option-confuses-me/#findComment-1152919 Share on other sites More sharing options...
Slips Posted December 30, 2010 Author Share Posted December 30, 2010 Nevermind guys, after doing some testing I figured that it is not possible to read the files contents and start writing it from the beginning of the file , with the same file handler. All those options that allow reading and writing, at best will write only from the end of the file. Quote Link to comment https://forums.phpfreaks.com/topic/222983-fopen-with-w-option-confuses-me/#findComment-1152923 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.