Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does reading from a php://memory wrapper I've just written to fail?

Tags:

php

I'm trying to read from the php://memory wrapper using fread() but fread() always returns false. My code is simplified:

$file_handle = fopen('php://memory', 'w+'); // Have tried php:temp also.
fwrite($file_handle, 'contents');
$file = fread($file_handle, filesize($file_handle)); // Have also tried 99999 for filesize.

$file always is false after the fread().

What's going on?

Thanks in advance!

like image 342
jimmy Avatar asked Apr 19 '13 21:04

jimmy


2 Answers

You'll need to rewind($file_handle) after writing before you can read what you've just written, because writing moves the file pointer to the end of the file

like image 156
Mark Baker Avatar answered Nov 14 '22 21:11

Mark Baker


Call rewind($file_handle) before fread()

like image 38
manish kumar Avatar answered Nov 14 '22 21:11

manish kumar