Jump to content

fread no output


kalster

Recommended Posts

i am writing to a file and then reading the contents of that file with "fopen w+", but i cant read the contents of that file. the output is blank. if i close the file and then reopen the file with the fopen "r" command then it works. why am i getting this strange behavior?

 

<?php
$do = "test.txt";
$file = fopen($do, 'w+') or die("file not found");
$go = "test";
fwrite($file, $go);
//fclose($file);
//$file1 = fopen($do, 'r') or die("file not found");
$re1 = fread($file, filesize($do)); 

fclose($file);
echo $re1;

?> 

Link to comment
Share on other sites

http://www.php.net/manual/en/function.fopen.php

 

If you read down to what the different modes are, you'll understand why.

 

r

Open for reading only; place the file pointer at the beginning of the file.

w+

Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

 

Use r+

 

r+

Open for reading and writing; place the file pointer at the beginning of the file.

Link to comment
Share on other sites

Disregard

 

Try this and tell us what happens.

 

<?php
$file_name = 'test.txt';
$file      = fopen($file_name, 'r+') or die ('File, "' . $file_name . '", was not found.');
$append    = 'test';
fwrite($file, $append);
$file_read = fread($file, filesize($file_name)); // Read a certain amount of bytes from the file (in this case, the entire file)

$file_get_all = file($file_name); // Read the entire file into an array. Each line is has it's own key.

print_r($file_read);
echo '<br />---<br /><pre>';
print_r($file_get_all);
echo '</pre>';
Link to comment
Share on other sites

Wait. I'm an idiot.

 

There is good reason for why it does not read as you expected.

 

When you write test to the file, it puts the pointer after the appended text. That means that it will output the EOF (end of file).

 

Either reset the pointer, or use file().

Link to comment
Share on other sites

Wait. I'm an idiot.

 

There is good reason for why it does not read as you expected.

 

When you write test to the file, it puts the pointer after the appended text. That means that it will output the EOF (end of file).

 

Either reset the pointer, or use file().

Or just use fclose() inbetween, which is always good practice to do after using fopen(). You can then re-open it and you should use the mode "r", to only read the file from the beginning of the file.

Link to comment
Share on other sites

I would recommend not using fopen (), fwrite () or fread () at all. Just file_get_contents () and file_put_contents instead, and you'll find this whole problem will go away by itself.

 

PS: Remember to use the correct flags, if you want to append instead of overwriting.

Link to comment
Share on other sites

ok, but what is the point of using "fopen r+" if it does not work in my case? that is, why use "fopen r+" at all?

 

When you write test to the file, it puts the pointer after the appended text. That means that it will output the EOF (end of file).

 

Either reset the pointer, or use file().

 

This is the file:

|Hello world from test.txt

 

When you use fwrite, it moves the pointer from the beginning of the file, to the end.

Hello world from test.txt|

 

It then writes the string to the file, leaving the pointer at the end.

Hello world from test.txtt|

Hello world from test.txtte|

Hello world from test.txttes|

Hello world from test.txttest|

 

To bring it back to the start, use rewind()

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.