Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Is it possible to create a SplFileObject object from file contents (string)?

Tags:

object

file

php

For example:

$contents = file_get_contents(image.png);

Is it possible to create a SplFileObject object from $contents?

Thanks!

like image 905
nut Avatar asked Jul 13 '13 02:07

nut


1 Answers

php has some special stream wrappers that let you re purpose the various file system functions

$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);

You don't need to use SplFileObject to use those wrappers though; you could use fopen and fwrite instead.

like image 151
goat Avatar answered Sep 17 '22 22:09

goat