Modes of fopen

Hello everybody,

I seemed a bit confused all of a sudden about the use of modes (the 2nd parameter) in fopen function in php

‘r’, ‘w’ seems to be ok, but im not sure about r+ and w+

Here is my code
1.
<?php
$fp = fopen(‘a.txt’,‘r+’);
$txt = fread($fp,filesize(‘a.txt’));
fwrite($fp,‘My text1’);
echo $txt;
fwrite($fp,‘My text2’);
echo $txt;
?>

As far as the manual says this code should read and write and it does. But as a bonus it is appending the ‘My text2’ text with ‘My text1’ which was beyond my imagination!!! Can anyone say why r+ is appending? It is nowhere written in the manual. I thought it should overwrite

<?php
$fp = fopen(‘a.txt’,‘w+’);
fwrite($fp,‘My text1’);
$txt = fread($fp,filesize(‘a.txt’));
echo $txt;
?>
And in this code, it echoes nothing and i thought w+ was for reading cum writing.

Guys hope you understand and follow me cause its terribly confusing at least to me.

Thanks in advance…

Rajug,

Thanks a lot. Its clear as daylight. I missed the part of closing the pointer.

Also can you possibly state me the difference between ‘w’ and ‘w+’ citing an example.

$fp = fopen('a.txt','w+');
fwrite($fp,'My text1');
$txt = fread($fp,filesize('a.txt'));
echo $txt;

Why am I not getting anything in $txt? It should read the newly written value. Or do i need to close my file pointer again? Again closing the pointer after writing and then re=opening it again in ‘w+’ mode will make my file empty.

$fp = fopen('a.txt','w+');
fwrite($fp,'My text1');
fclose($fp);
$fp = fopen('a.txt','w+');
$txt = fread($fp,filesize('a.txt'));
fclose($fp);
echo $txt;

If so then what is then how come they are saying it read+write (for w+) when we can do only one at a time? (even how can i read using ‘w+’)

I hope im not bothering you guys too much with my silly questions…

:frowning:

Oh sorry… i just thought it was ‘a+’.

Edit:

Your first script is something like this:


$fp = fopen('a.txt','r+');
fwrite($fp,'My text1');
fwrite($fp,'My text2');
fwrite($fp,'My text3');

if you write the text without closing pointer then it will treat just like a single write statement as far as i know. So your above code is same as below:


$fp = fopen('a.txt','r+');
fwrite($fp,'My text1My text2My text3');

This code is like your above code. But the manual what it says is true if you close the pointer and open it again then it ‘r+’ will not append the text. It will overwrite the new text in your file:

So consider the following once:


$fp = fopen('a.txt','r+');
fwrite($fp,'My text1');
fclose($fp);

$fp = fopen('a.txt','r+');
fwrite($fp,'My text2');
fclose($fp);

$fp = fopen('a.txt','r+');
fwrite($fp,'My text3');
fclose($fp);

// now read the file
$fp = fopen('a.txt', 'r+');
echo fread($fp,filesize('a.txt'));
fclose($fp);

I hope this will make you little clear.

Difference between ‘w’ and ‘w+’ is stated clearly in a single sentence in the manual:
‘w’ = for only writing
‘w+’ = for both writing and reading

So about writing the content, please read the manual:
http://www.php.net/manual/en/function.fwrite.php

Think of a file’s contents as a series of bytes. The first byte is located at position 0, and the position of the last byte depends on how much data is in the file. When dealing with ascii characters, 1 byte usually represents 1 character*.

When you have an open resource handle to a file, there’s a hidden position value that php maintains for that handle. Even if you have multiple handles open for the same file, the position is kept separate for each handle returned by each call to fopen().

Most of the f*() functions like fread/fwrite use this hidden position value to perform their functionality. For example, if you call fread($fp, 10) it will read 10 bytes, starting at the current position, and then advance the current position by 10 bytes. Same with fwrite, calling fwrite($fp, ‘hi’) will write 2 bytes starting from the current position, and then advance the position by 2 bytes.

You can set the position to wherever you like by using fseek(). You can find out the current position by using ftell(). Depending on the mode you open the file handle with, php might initially set the position to the end, or the start etc…

The different modes for fopen() do behave differently though. It’s been a while since I’ve played with it, but for example I think with a+, the position always starts at 0, and advances as expected when reading, but any writing is always done to the end of the file, and doesn’t advance the position.

So, for this


$fp = fopen('a.txt','w+'); 
//echo ftell($fp); should say 0 right now

fwrite($fp,'My text1'); 
//now ftell should say 8

$txt = fread($fp,filesize('a.txt')); 
// filesize is 8 bytes, but, you're trying to read 8 bytes starting at position 8. 
// you would need to first fseek() to position 0, and then you can read 8 bytes

  • It’s bad to think of a character as a byte, because it depends on which character and which character set, and encoding you’re using. This is a large complicated topic that I’m going to avoid. I’m only making this poor assumption in this post because it makes it simple to explain how the file handle position works.

Hey rajug,

i follow u here…

but how does ‘place the file pointer at the beginning of the file’ means the pointer will be at the end of the file??? It says beginning …

am i loosing u somewhere?