我试图使用fread()
从php://memory
包装器读取,但fread()
总是返回false。我的代码被简化了:
$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
总是在fread()
之后为假。
怎么回事?
提前感谢!
您需要在写入后倒带($file_handle),然后才能读取刚刚写入的内容,因为写入将文件指针移动到文件的末尾
在fread()
之前调用rewind($file_handle)
在这里找到答案:https://stackoverflow.com/a/2987330/2271704
我需要在调用fread()
之前调用rewind($file_handle)
。
你应该得到这个:
警告:filesize()期望参数1是字符串,资源给定
这是第一个问题:
int filesize ( string $filename )
- 获取给定文件的大小。
…第二个:你没有启用错误报告。
第三个问题是指针在文件的末尾。试试这样做:
error_reporting(E_ALL);
ini_set('display_errors', true);
// ...
rewind($file_handle);
$file = ''; //
while (!feof($file_handle)) {
$file .= fread($file_handle, 8192);
}