Jump to content

fopen with 'w+' option confuses me


Slips

Recommended Posts

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 :D

 

Thanks

 

Link to comment
Share on other sites

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()

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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