例如:
$contents = file_get_contents(image.png);
可以从$contents创建SplFileObject对象吗?
谢谢!
PHP有一些特殊流包装器,可以让您重新使用各种文件系统功能。
$contents = 'i am a string';
$file = 'php://memory'; //full memory buffering mode
//$file = 'php://temp/maxmemory:1048576'; //partial memory buffering mode. Tries to use memory, but over 1MB will automatically page the excess to a file.
$o = new SplFileObject($file, 'w+');
$o->fwrite($contents);
你并不需要使用 SplFileObject 去使用那些包装器;相反,你可以使用 fopen 和 fwrite。
php://temp
,然后作为 SplFileObject 读取,但是您的代码显示了一个非常重要的问题:在 SplFileObject 中创建包装器后,需要先将内容写入该包装器。 - Benj$fp = fopen('php://temp', 'w'); $id = (int) $fp;
然后使用 new SplFileObject("php://fd/$id", 'r')
可能有效进行读取。一些信息:http://php.net/manual/en/wrappers.php.php - goat
new SplFileObject('image.png');
。 - goat