使用php://内存包装器会导致错误

Hor*_*Kol 4 php wrapper

我试图通过添加一个允许我使用字符串数据而不是文件路径添加附件的方法来扩展来自Worx的PHP邮件程序类.

我提出了这样的事情:

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);
}
Run Code Online (Sandbox Code Playgroud)

但是,我得到的只是一个PHP警告:

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

原始文档没有任何体面的例子,但我在互联网上发现了一对(包括一个在SO上),根据它们我的用法看起来是正确的.

有没有人使用过这个?

我的另一种方法是创建一个临时文件并清理 - 但这意味着必须写入光盘,并且此函数将用作大批量进程的一部分,我希望尽可能避免慢速光盘操作(旧服务器).这只是一个短文件,但脚本电子邮件的每个人都有不同的信息.

Art*_*cto 17

这只是php://memory.例如,

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

产生"bugabuga".


Gav*_*gan 2

快速查看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";
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)