Reading and writing to the same file

I am trying to read and write to / from the same file, is this possible?

Here is what I get the negative results:

<?php
$file = fopen("filename.csv", "r") or exit("Unable to open file!");

while (!feof($file)) {
    $line = fgets($file);
    fwrite($file,$line);
}

fclose($file);
?>
+3
source share
3 answers

You open the file in read-only mode. If you want to also write to a file, do fopen("filename.csv", "r+")

+9
source

You will need to open the file with more than "r +" instead of "r". See the fopen documentation: http://php.net/manual/en/function.fopen.php

+6
source

You opened the file in read-only mode. See docs .

$file = fopen("filename.csv", "r+") or exit("Unable to open file!");
+5
source

Source: https://habr.com/ru/post/1789914/


All Articles