Using php: // memory covers causes errors

I am trying to extend the PHP mailer class from Worx by adding a method that allows me to add attachments using string data, rather than the file path.

I came up with something like this:

public function addAttachmentString($string, $name='', $encoding = 'base64', $type = 'application/octet-stream')
{
  $path = 'php://memory/' . md5(microtime());
  $file = fopen($path, 'w');
  fwrite($file, $string);
  fclose($file);

  $this->AddAttachment($path, $name, $encoding, $type);
}

However, all I get is a PHP warning:

PHP Warning:  fopen() [<a href='function.fopen'>function.fopen</a>]: Invalid php:// URL specified

There are no decent examples with the original documentation, but I found a couple on the Internet (including one here on SO ), and my usage is displayed according to them.

Has anyone had success using this?

- , , ( ) . , script.

+3
3

http://php.net/manual/en/wrappers.php.php , "/". md5 (microtime ( ));" .

:

<?php
print "Trying with md5\n";
$path = 'php://memory/' . md5(microtime());
$file = fopen($path, 'w');
if ($file)
{
    fwrite($file, "blah");
    fclose($file);
}
print "done - with md5\n";

print "Trying without md5\n";
$path = 'php://memory';
$file = fopen($path, 'w');
if ($file)
{
    fwrite($file, "blah");
    fclose($file);
}
print "done - no md5\n";

:

buzzbee ~$ php test.php 
Trying with md5

Warning: fopen(): Invalid php:// URL specified in test.php on line 4

Warning: fopen(php://memory/d2a0eef34dff2b8cc40bca14a761a8eb): failed to open stream: operation failed in test.php on line 4
done - with md5
Trying without md5
done - no md5
+1

php://memory. ,

<?php
    $path = 'php://memory';
    $h = fopen($path, "rw+");
    fwrite($h, "bugabuga");
    fseek($h, 0);
    echo stream_get_contents($h);

"bugabuga".

+15

- :

php://memory php://temp -, . , php://memory , php://temp , ( - 2 ). , sys_get_temp_dir().

, temp , :

php://temp/maxmemory:$limit

$limit . , .

+1

All Articles