【发布时间】:2014-08-23 07:40:31
【问题描述】:
我试图在我的网站上添加一项功能,允许用户在发送电子邮件时添加附件。我用 PHP 试过这个,但代码不起作用。它既没有上传附件,也没有在我的数据库中插入附件的 URL,也没有回显确认消息。
除附件外,其他所有表单代码均已成功处理。请建议我应该对代码进行哪些更改:
PHP:
if (isset($_POST['attachment']))
{
if ($_FILES['file']['size'] > 52428800)
{
$attachment_results = "Sorry, your attachment could not be processed as it is exceeding the limit of 50 MB.";
}
else
{
if ($_FILES["file"]["error"] > 0)
{
$attachment_results = "Sorry, your attachment could not be processed due to some error. Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$attachment_results = "Your attachment was also processed successfully.<br>
File name: " . $_FILES['file']['name'] . "<br>Attachment Size: " . $_FILES['file']['size'];
$destination = "attachments/" . $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['name'], $destination);
$url = "https://$domain_name/$destination";
}
}
echo $attachment_results;
}
稍后在脚本中,$url 将被插入到 MySQL 数据库中,但这也不起作用。只是在数据库中插入了一个空白变量。
这是 HTML 代码:
<form enctype="multipart/form-data" method="post" action="">
<input type="file" name="attachment" style="color: #000000;"><label>(Maximum 50 MB) </label>
<!-- Other Form Code -->
</form>
【问题讨论】:
-
让你给 0777 目录权限
-
我没有.. 怎么办??它需要一些其他的PHP代码吗??
-
你可以通过 ftp 客户端来实现...而且,我肯定会使用一些 php 库(例如 phpmailer)来发送带有附件的电子邮件 -> 没有它会很痛苦...
-
你用的是linux还是windows
-
我使用的是 windows 8 64 位..
标签: php email attachment