Why is reading from the php: // memory wrapper that I just wrote not working?

I try to read from a wrapper php://memoryusing 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.

$filealways false after fread().

What's happening?

Thanks in advance!

+5
source share
4 answers

After writing, you will need to rewind ($ file_handle) before you can read what you just wrote, because writing moves the file pointer to the end of the file

+10
source

Found the answer here: fooobar.com/questions/1153806 / ...

I need to call rewind($file_handle)before I call fread().

+1
source

:

: filesize() , 1 ,

:

int filesize ( string $filename ) - .

... : .

The third problem is that your pointer is at the end of the file. Try something like this:

error_reporting(E_ALL);
ini_set('display_errors', true);
// ...

rewind($file_handle);
$file = ''; //
while (!feof($file_handle)) {
    $file .= fread($file_handle, 8192);
}
0
source

Call rewind($file_handle)upfread()

0
source

All Articles