使用 php://memory 包装器会导致错误 [英] Using a php://memory wrapper causes errors

查看:31
本文介绍了使用 php://memory 包装器会导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想出了这样的事情:

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 警告:

However, all I get is a PHP warning:

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

原始文档中没有任何像样的示例,但我在互联网上找到了一些示例(包括 SO 上的一个),根据他们我的用法似乎是正确的.

There aren't any decent examples with the original documentation, but I've found a couple around the internet (including one here on SO), and my usage appears correct according to them.

有人成功使用过这个吗?

Has anyone had any success with using this?

我的替代方法是创建一个临时文件并清理 - 但这意味着必须写入磁盘,并且此功能将用作大型批处理过程的一部分,我想避免缓慢的磁盘操作(旧服务器)在可能的情况.这只是一个简短的文件,但脚本通过电子邮件发送给每个人的信息都不同.

My alternative is to create a temporary file and clean up - but that will mean having to write to disc, and this function will be used as part of a large batch process and I want to avoid slow disc operations (old server) where possible. This is only a short file but has different information for each person the script emails.

推荐答案

速看http://php.net/manual/en/wrappers.php.php 和源代码,我看不到对/' . md5(microtime());"的支持位.

Quickly looking at http://php.net/manual/en/wrappers.php.php and the source code, I don't see support for the "/' . md5(microtime());" bit.

示例代码:

<?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

这篇关于使用 php://memory 包装器会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆