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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.