Home > Software engineering >  File content is not available after fputcsv
File content is not available after fputcsv

Time:01-29

Please explain to me why the function file_get_contents returns the contents of the file, while fread does not. How can I read file content after fputcsv?

$file = fopen('test.txt', 'r ');
fputcsv($file, [1, 2, 4]);
var_dump(fread($file, 1024));
var_dump(file_get_contents('test.txt'));

CodePudding user response:

fread() reads from the current position in the file stream, which is after what you just wrote. You need to seek to the beginning to read the file.

$file = fopen('test.txt', 'r ');
fputcsv($file, [1, 2, 4]);
fseek($file, 0);
var_dump(fread($file, 1024));
  • Related