P1nGu1n
P1nGu1n

Reputation: 594

File IO in PHP doens't work

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.

Upvotes: 2

Views: 361

Answers (2)

Baba
Baba

Reputation: 95121

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

Upvotes: 8

Lobo
Lobo

Reputation: 4157

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.

Upvotes: -2

Related Questions