我正在尝试通过添加一种方法来扩展 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);
}
但是,我得到的只是一个 PHP 警告:
PHP Warning: fopen() [<a href='function.fopen'>function.fopen</a>]: Invalid php:// URL specified
原始文档中没有任何像样的示例,但我在互联网上找到了几个示例(包括 one here on SO ),根据它们,我的用法似乎是正确的。
有没有人成功使用过这个?
我的替代方案是创建一个临时文件并清理 - 但这意味着必须写入光盘,并且此功能将用作大型批处理过程的一部分,我想避免缓慢的光盘操作(旧服务器)在可能的情况。这只是一个简短的文件,但对于脚本电子邮件中的每个人都有不同的信息。
最佳答案
只是php://memory
。例如,
<?php
$path = 'php://memory';
$h = fopen($path, "rw+");
fwrite($h, "bugabuga");
fseek($h, 0);
echo stream_get_contents($h);
产生“bugabuga”。
关于php - 使用 php ://memory wrapper causes errors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2987265/