php读写文件

function readWrite() {
    $fileReader = fopen('file.txt', 'r') or die("ERROR: File not found");
    $fileWriter = fopen('filewr.txt', 'w') or die ("ERROR: Write File not 
    found");
    $totalDaysArr= array();
    $monthNumArr= array();
    $monthArr= array();
    $row= file("file.txt");

    while($row = fgets($fileReader, 4096)) {
      list($monthNumArr[], $monthArr[], $totalDaysArr[]) = explode(",", $row);
    }

    for($x = 11; $x >= 0; $x--)
    {
      $table = explode(",", $monthNumArr[$x]);
      fwrite($fileWriter, $row[$x]);
    }
    fclose($fileWriter);
}

I'm a beginner at PHP, so this is what I have so far. My objective is to make a function that will read a file called file.txt and then write it in reverse using arrays. I'm not sure what I am doing wrong here.

These are the requirements:

This function should use PHP array to store records (each line is a "record").

This function should use a PHP Loop to walk through the months names array and generate HTML5/CSS to display the table.

Create a function that writes the text file filewr.txt (reverse order).

This function should use PHP for loop to walk through the array in reverse order and write each array entry (line/record) to the filewr.txt file.

and also the txt file looks like this:

1,January,31

2,February,28

3,March,31

4,April,30

5,May,31

6,June,30

7,July,31

8,August,31

9,September,30

10,October,31

11,November,30

12,December,31
  1. Why use fileReader when you decide to read the data with file("file.txt"); in the end?
  2. The writing process is quite messed up fwrite($fileWriter, $row[$x]) is quite possibly the place where the error comes from: don't you aim to write that table to your file instead of a row from the original input file?

Addition

@Aureliux: What you want to to is generating a HTML string. Therefore, lets start with $table='<table>' and put this definition before the loop. After the loop you add the tables closing tag: $table.='</table>' The magic happens in between: For each record you create the corresponding HTML representation. $table.='<tr><td>'.$monthNumArr.'</td><td>'.$monthArr.'</td><td>'.$totalDaysArr.'</td></tr>'. Finaly you move the write command after the loop and write the generated Table Markup to your output file.

What you will end up with looks like

function readWrite() {
    $fileWriter = fopen('filewr.txt', 'w') or die ("ERROR: Write File not 
    found");
    $totalDaysArr= array();
    $monthNumArr= array();
    $monthArr= array();
    $row= file("file.txt");

    while($row = fgets($fileReader, 4096)) {
      list($monthNumArr[], $monthArr[], $totalDaysArr[]) = explode(",", $row);
    }

    $table='<table>';
    for($x = 11; $x >= 0; $x--)
    {
      $table.='<tr><td>'.$monthNumArr.'</td><td>'.$monthArr.'</td><td>'.$totalDaysArr.'</td></tr>';
    }
    $table.='</table>';
    fwrite($fileWriter, $table);
    fclose($fileWriter);
}

I have no idea why you would need the reverse loop, but actually, you could achieve the same result with less effort:

function readWrite() {
    $row= file("file.txt");

    $table='<table>';
    for($x = 11; $x >= 0; $x--)
    {
      $table.='<tr><td>'.implode('</td><td>', explode(',',$row[$x])).'</td></tr>';
    }
    $table.='</table>';
    file_put_contents('file.txt', $table);
}

I did not test it. But the idea should be clear. Personally, I would agree with @Alive to Die. His approach is the most straight forward.