Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

File content is not available after fputcsv

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading