2

For a school assignment I have to write a script in PHP. It has to create an array with names and write the array to a file. Then it has to delete the array, read the names in the file and the echo them.

This doesn't work:

<?php
    $names = array("Name 1", "Name 2", "Name 3", "Name 4", "Name 5");
    $filename = "names.txt";
    $handle = fopen($filename, "wb+"); //open as read and write
    $string = implode("\r\n", $names); //name on each line
    fwrite($handle, $string); //write line

    $names = array(); //empty array
    print_r($names); //print empty array
    $string = fread($handle, filesize($filename)); //read file
    $names = explode("\r\n", $string); //lines to array
    foreach ($names as $name) {
        echo("<br>\n$name");
    }
    fclose($handle);
?>

But this works:

<?php
    $names = array("Name 1", "Name 2", "Name 3", "Name 4", "Name 5");
    $filename = "names.txt";
    $handle = fopen($filename, "wb"); //open as write
    $string = implode("\r\n", $names); //name on each line
    fwrite($handle, $string); //write line
    fclose($handle); //close

    $handle = fopen($filename, "r"); //open as read
    $names = array(); //empty array
    print_r($names); //print empty array
    $string = fread($handle, filesize($filename)); //read file
    $names = explode("\r\n", $string); //lines to array
    foreach ($names as $name) {
        echo("<br>\n$name");
    }
    fclose($handle);
?>

I don't get why it works when I close and reopen the file. Any help would be appreciated.

3
  • The second script is correct, not first.
    – Lobo
    Dec 4, 2012 at 14:47
  • 1
    It's much easier to work with file_get_contents and file_put_contents rather than using the whole fread/filesize/fclose ordeal.
    – N.B.
    Dec 4, 2012 at 14:48
  • @N.B. Thanks for the suggestion, but the chapter was about fopen, fwrite and so on
    – P1nGu1n
    Dec 4, 2012 at 14:59

2 Answers 2

8

All you need is to restart the file pointer because when using fwrite():

fwrite() writes the contents of string to the file stream pointed to by handle.

  • length bytes have been read
  • EOF (end of file) is reached
  • a packet becomes available or the socket timeout occurs (for network streams)
  • if the stream is read buffered and it does not represent a plain file, at most one read of up to a number of bytes equal to the chunk size (usually 8192) is made; depending on the previously buffered data, the size of the returned data may be larger than the chunk size.

if you look at fread()

fread() reads up to length bytes from the file pointer referenced by handle. Reading stops as soon as one of the these conditions is met:

So at at the time you are calling fread($handle, filesize($filename)); you are already at the end of file therefore nothing would be displayed.

To resolve this just use fseek to restart the pointer back to the beginning of the file

Example :

 fseek($handle, 0);

Yon't don't need to close the file .. your first code would work perfectly

Edit

You can also you rewind from PHP DOC

Sets the file position indicator for handle to the beginning of the file stream.

 rewind($handle);
2
  • Thanks it worked! I now understand better how reading and writing works.
    – P1nGu1n
    Dec 4, 2012 at 14:58
  • Good to know i was able to help
    – Baba
    Dec 4, 2012 at 14:58
-2

The second script is correct. The reason is that if you open a file for writing, you can not use to read, so you have to close the file and reopen it in read.

1
  • 2
    -1. false. w+ is reading and writing, but when the write is complete, the file pointer is at EOF and there's nothing to read.
    – Marc B
    Dec 4, 2012 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.