读取/编写php:// temp流时遇到问题

问题描述 投票:24回答:4

我在读写PHP 5.3.2中的php://temp流时遇到麻烦

我基本上有:

file_put_contents('php://temp/test', 'test');
var_dump(file_get_contents('php://temp/test'));

我得到的唯一输出是string(0) ""

我不应该把我的“测试”回来吗?

php stream
4个回答
22
投票
文件路径,它是一个伪协议,在使用时总是创建一个新的随机临时文件。实际上/test被完全忽略。 php://temp包装器接受的唯一额外“参数”是/maxmemory:n。您需要保留打开的临时流的文件句柄,否则它将被丢弃:

$tmp = fopen('php://temp', 'r+'); fwrite($tmp, 'test'); rewind($tmp); fpassthru($tmp); fclose($tmp); 请参见http://php.net/manual/en/wrappers.php.php#refsect1-wrappers.php-examples


10
投票

1
投票
$handle = fopen('php://temp', 'w+'); fwrite($handle, 'I am freaking awesome'); fread($handle); // returns ''; rewind($handle); // resets the position of pointer fread($handle, fstat($handle)['size']); // I am freaking awesome

1
投票
[Example 5 at the PHP Manual]使用几乎完全相同的代码样本并说

php:// memory和php:// temp不可重用,即在流之后已关闭,无法再次引用它们。

file_put_contents('php://memory', 'PHP'); echo file_get_contents('php://memory'); // prints nothing

我想这意味着file_put_contents()在内部关闭流,这使file_get_contents()无法再次恢复流中的数据

© www.soinside.com 2019 - 2024. All rights reserved.