【问题标题】:PHP Send file in HTML form to mailPHP以HTML格式发送文件到邮件
【发布时间】:2014-06-08 05:50:51
【问题描述】:

我希望使用 html 表单发送 2 个附加到电子邮件的文件。邮件与 2 个文件一起发送,但我下载时看不到文件(大小:0 ko)。 你不能帮我吗?这是我的php文件的代码:

$boundary = "-----=".md5(uniqid(rand()));

$header  = "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";
$header .= "\r\n";

$msg = "Je vous informe que ceci est un message au format MIME 1.0 multipart/mixed.\r\n";

$msg .= "--$boundary\r\n";
$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$msg .= "Content-Transfer-Encoding:8bit\r\n";
$msg .= "\r\n";
$msg .= "Ceci est un mail avec 2 fichiers joints\r\n";
$msg .= "\r\n";

$file = $_FILES['icone']['name'];
$fp   = fopen($file, "rb");   // le b c'est pour les windowsiens
$attachment = fread($fp, filesize($file));
fclose($fp);
$attachment = chunk_split(base64_encode($attachment));

$msg .= "--$boundary\r\n";
$msg .= "Content-Type: multipart/mixed; name=\"$file\"\r\n";
$msg .= "Content-Transfer-Encoding: base64\r\n";
$msg .= "Content-Disposition: attachment; filename=\"$file\"\r\n";
$msg .= "\r\n";
$msg .= $attachment . "\r\n";
$msg .= "\r\n\r\n";

$file = $_FILES['nom_fichier']['name'];
$fp = fopen($file, "rb");
$attachment = fread($fp, filesize($file));
fclose($fp);
$attachment = chunk_split(base64_encode($attachment));

$msg .= "--$boundary\r\n";
$msg .= "Content-Type: multipart/mixed; name=\"$file\"\r\n";
$msg .= "Content-Transfer-Encoding: base64\r\n";
$msg .= "Content-Disposition: attachment; filename=\"$file\"\r\n";
$msg .= "\r\n";
$msg .= $attachment . "\r\n";
$msg .= "\r\n\r\n";

$msg .= "--$boundary--\r\n";

$destinataire = "jeanb@hotmail.com";
$expediteur   = $_POST['email'];
$reponse      = $expediteur;
echo "Ce script envoie un mail avec 2 fichiers joints à $destinataire";
mail($destinataire,
     "Email avec 2 fichiers joints (dont 1 inline)",
     $msg,
     "Reply-to: $reponse\r\nFrom: $destinataire\r\n".$header);

警告信息是:

警告:fopen(date horloge.txt) [function.fopen]: 未能打开 流:中没有这样的文件或目录 /home/public_html/mail_candidat.php 在第 34 行

警告:filesize() [function.filesize]: stat failed for date /home/public_html/mail_candidat.php 第 35 行中的 horloge.txt

警告:fread():提供的参数不是有效的流资源 /home/public_html/mail_candidat.php 第 35 行

警告:fclose():提供的参数不是有效的流资源 在 /home/public_html/mail_candidat.php 第 36 行

Ce script envoie un mail avec 2 fichiers Joint à jeanb@hotmail.com

【问题讨论】:

  • 在你的问题中使用英语。 ich antworte ja auch nicht auf deutsch.
  • 嗯,错误信息清楚地告诉您,您正在尝试打开并读取一个不存在的文件。您的路径指向没有文件。这很可能是这里的 path 问题。您的当前工作目录是什么?
  • 我希望用户上传我在电子邮件中直接收到的文件。我没有“当前工作目录”……有可能吗?

标签: php html file email join


【解决方案1】:

文件的路径存储在“tmp_name”中,而不是名称中。命名文件的调用方式,而不是文件的路径。

在你的例子中就是这样。

之前

$file = $_FILES['nom_fichier']['name'];
$fp = fopen($file, "rb");
$attachment = fread($fp, filesize($file));
fclose($fp);

之后

$file = $_FILES['nom_fichier']['tmp_name'];
$fp = fopen($file, "rb");
$attachment = fread($fp, filesize($file));
fclose($fp);

【讨论】:

  • 感谢您的回答,我已经完成了,但我再次收到一个空文件(文件大小为 0 ko)。你现在有这个问题吗?
  • 您可以显示您在发布到此脚本时使用的 html 表单吗?
  • 这里是表格
【解决方案2】:

fopen 找不到文件(没有这样的文件或目录)。 所有其他警告都与此相关。

检查返回的路径,看看它是否返回一个有效的路径。

【讨论】:

  • 我想通过我的 php 文件将文件直接从 html 表单发送到电子邮件。我不知道如何检查路径(有效与否)。你能帮忙吗?
【解决方案3】:

您有一个工作目录(这是您的 php 文件所在的目录),在这种情况下是:/home/public_html/ 但您必须知道您是否有权将文件上传到此目录,或者您有一个 tmp 或任何其他可写目录。

像这样扩展你的代码:

$file =  basename($_FILES['icone']['name']);
if (!move_uploaded_file($_FILES['icone']['tmp_name'], $file)) {    echo "Error";} 
echo "file: ".$file; //For debug only
$fp = fopen($file, "rb");
$attachment = fread($fp, filesize($file));
fclose($fp);
echo $attachment; //For debug only

如果不起作用,请评论警告或错误消息。

我看到你没有上传文件。

您的错误同样是“无法打开流:/home/public_html/mail_candidat.php 中没有这样的文件或目录”

在发送之前尝试上传文件:

http://www.php.net/manual/en/features.file-upload.post-method.php

【讨论】:

    猜你喜欢
    • 2017-06-28
    • 2013-10-18
    • 2010-11-26
    • 2014-06-22
    • 1970-01-01
    • 2015-07-19
    相关资源
    最近更新 更多