我已将此添加到我的form_action.php:
if(isset($_FILES['attachment1'])){
$errors= array();
$file_name = $_FILES['attachment1']['name'];
$file_size = $_FILES['attachment1']['size'];
$file_tmp = $_FILES['attachment1']['tmp_name'];
$file_type = $_FILES['attachment1']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['attachment1']['name'])));
$expensions= array("jpeg","jpg","png","doc","pdf");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG, PNG, PDF or DOC file.";
}
if(empty($errors)==true) {
move_uploaded_file($file_tmp,"uploads/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
表单字段为
<form method="post" action="form_action1.php">
<div class="form-group">
<label for="attachment1">Add file(s)</label>
<input type="file" class="form-control-file" id="attachment1" aria-describedby="fileHelp" name="attachment1">
<input type="file" class="form-control-file" id="attachment2" aria-describedby="fileHelp" name="attachment2">
<input type="file" class="form-control-file" id="attachment3" aria-describedby="fileHelp" name="attachment3">
</div>
</form>
路径已保存,但文件未上传。我错过了一个我不知道的步骤吗?另外,我可以在$ _FILES字段中设置ARRAY以允许三次上传吗?
这是我想要完成的个人项目的最后一步。谢谢。
答案 0 :(得分:2)
您需要为表单添加enctype
以发布文件。
<form method="post" action="form_action1.php" enctype="multipart/form-data">
来自MDN:
<强>
enctype
强>当method属性的值为post时,enctype是用于将表单提交给服务器的MIME内容类型。可能的值是: application / x-www-form-urlencoded:如果未指定属性,则为默认值。
multipart/form-data:
用于type属性设置为“file”的元素的值。text/plain
(HTML5):此值可以被或元素上的formenctype属性覆盖。