Jump to content

php://memory wrapper


448191

Recommended Posts

I recently looked at the php://memory and php://temp wrappers, but I fail to see the use of it. I was hoping (idle hope) these would persist across requests, so I could utilize them for session management, but it gets terminated with execution environment of each request, so no go.

 

Anybody has any idea how these wrappers might be useful?

Link to comment
https://forums.phpfreaks.com/topic/52950-phpmemory-wrapper/
Share on other sites

  • 1 year later...

Hah. Came across this ancient post and decided to answer my own question.

 

I've recently found a use for these wrappers: to use with file manipulation functions.

 

Example:

 

/**
* Private string parser for factory method
*
* @param string $string
* @param string $delimiter
* @param string $enclosure
* 
* @return array
*/
private static function _parseString($string, $delimiter, $enclosure)
{
$maxMemAllocation = 2 * 1024 * 1024;

$handle = fopen("php://temp/maxmemory:$maxMemAllocation", 'r+');
fwrite($handle, $string);
rewind($handle);

$rows = array();

while(false !== ($row = fgetcsv($handle, null, $delimiter, $enclosure)))
{
	$rows[] = $row;
}

$keys = array_shift($rows);

foreach($rows as $row)
{
	$assocRows[] = array_combine($keys, $row);
}
return $assocRows;
}

 

There's no string counterpart of fgetcsv (str_getcsv is only in CVS), so this saves me a little trouble not having to write csv parsing code, with hardly any performance loss, possibly none.

Link to comment
https://forums.phpfreaks.com/topic/52950-phpmemory-wrapper/#findComment-549794
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.