我试图在我的网站上添加一个功能,允许用户在发送电子邮件时添加附件。我尝试了这个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>
<?php
if (isset($_POST['submit']))
{
print_r($_FILES);
if ($_FILES['attachment']['size'] > 52428800)
{
$attachment_results = "Sorry, your attachment could not be processed as it is exceeding the limit of 50 MB.";
}
else
{
if ($_FILES["attachment"]["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['attachment']['name'] . "<br>Attachment Size: " . $_FILES['file']['size'];
$destination = "attachments/" . $_FILES['attachment']['name'];
move_uploaded_file($_FILES['attachment']['tmp_name'], $destination);
$url = "https://$domain_name/$destination";
}
}
echo $attachment_results;
}
?>
<form enctype="multipart/form-data" method="post" action="">
<input type="file" name="attachment" style="color: #000000;"><label>(Maximum 50 MB) </label>
<input type="submit" name="submit">
</form>