php 表单提交后 $_FILES 为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15130159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$_FILES empty after form submission
提问by Ortund
I'm using standard PHP functions to upload a file for use as an attachment with PHPMailer.
我正在使用标准的 PHP 函数上传一个文件,用作 PHPMailer 的附件。
<form name="upload" method="post" action="send_form_email3.php">
<div width="100%" class="con3">
<div class="lable">
<label for="first_name">First Name *</label>
</div>
<input type="text" name="first_name" id="first_name" class="span4">
<div class="lable">
<label for="email">Email Address *</label>
</div>
<input type="text" name="email" id="email" class="span4">
<div class="lable">
<label for="telephone">Contact Number *</label>
</div>
<input type="text" name="telephone" id="telephone" class="span4">
<div class="lable">
<label for="comments">Message *</label>
</div>
<textarea name="comments" rows="8" id="comments" class="span4"></textarea>
<div class="lable">
<label for="upload">Send Us Your CV *</label>
</div>
<input type="file" name="upload" id="upload" />
<input type="submit" value="Submit" class="btn btn-success">
</div>
</form>
This form gets submitted to the following php handler where the mail is built and sent:
此表单被提交到以下 php 处理程序,在其中构建和发送邮件:
<?php
require_once("class.phpmailer.php");
$first_name = $_POST['first_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // required
$comments = $_POST['comments']; // required
echo "just got form values<br />";
echo $_FILES['upload']['name'].'<br />';
$email_message = "Form details below.<br />";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Contact: ".clean_string($telephone)."\n";
$email_message .= "Message:\n".clean_string($comments)."\n";
echo "added text to email<br />";
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['upload']['name']);
// upload the file
echo "target = ".$target_path."<br />";
if (isset($_FILES['upload']['size']))
{
if ($_FILES['upload']['size'] > 0)
{
echo 'file size: '.basename($_FILES['upload']['size']).'<br />';
if (move_uploaded_file($_FILES['upload']['name'], $target_path))
{
echo "The file ". basename( $_FILES['upload']['name'])." has been uploaded<br />";
// adding an already existing file as an attachment to the email for debugging purposes.
$email->AddAttachment('uploads/CreditReportViewer.pdf');
}
else
{
echo "There was an error uploading the file, please try again!<br /> ".basename($_FILES['upload']['error']);
}
}
else
{
echo "There was an error uploading the file, please try again!<br />";
}
}
else
{
echo "No file was found for the upload.<br />";
}
$email = new PHPMailer();
$email->To = "[email protected]";
$email->From = $email_from;
$email->FromName = $first_name;
$email->Subject = "New Message from Website";
$email->Body = $email_message;
echo "\n mail built...<br />";
$email->Send();
echo "mail sent!";
?>
The problem is that $_FILES['upload']['name']is not set. Here's the echoes that are being written to the browser:
问题是$_FILES['upload']['name']没有设置。这是写入浏览器的回声:
just got form values
added text to email
target = uploads/
No file was found for the upload.
mail built...
mail sent!
刚刚得到表单值
添加文本到电子邮件
目标 = 上传/
未找到要上传的文件。
邮件已建立...
邮件已发送!
This makes me think that I'm referencing the file upload field or the upload itself incorrectly.
这让我觉得我错误地引用了文件上传字段或上传本身。
Can anyone see any problems here or provide some guidance if this isn't the best way to do this?
如果这不是最好的方法,任何人都可以在这里看到任何问题或提供一些指导吗?
回答by Edwin Alex
You have to add,
你必须补充,
enctype= "multipart/form-data"
Change your form to,
将您的表格更改为,
<form name="upload" method="post" action="send_form_email3.php" enctype= "multipart/form-data">
回答by Sudhir Bastakoti
add enctypeto your form:
change:
添加enctype到您的表单:更改:
<form name="upload" method="post" action="send_form_email3.php">
to
到
<form name="upload" method="post" action="send_form_email3.php" enctype="multipart/form-data">
回答by celsowm
Another tip: if the upload_max_filesize(php.ini) is less than the file selected by user, $_FILES will be empty.
另一个提示:如果upload_max_filesize(php.ini) 小于用户选择的文件,$_FILES 将为空。
回答by Dipesh Parmar
You forget to set multipart in form declaration.
您忘记在表单声明中设置 multipart。
add enctype="multipart/form-data"in form.
加入enctype="multipart/form-data"表格。
回答by Techie
You should add enctype=multipart/form-datato your form.
您应该添加enctype=multipart/form-data到您的表单中。
<form name="upload" method="post" action="send_form_email3.php" enctype="multipart/form-data">
Because it's transmitted as a MIME stream, a very different format than a normal POST
因为它是作为 MIME 流传输的,与普通的 POST 格式非常不同
回答by Maoritzio
If you added the enctype and still doesn't work - check the PHP file permissions. in ooowebhost you can find it in chmod. change to '777'. otherwise, the file doesn't have permissions to execute/write
如果您添加了 enctype 并且仍然不起作用 - 检查 PHP 文件权限。在 ooowebhost 中,您可以在 chmod 中找到它。更改为“777”。否则,该文件没有执行/写入权限

