我正在尝试从表单上传图片,但是我一直收到错误:未定义索引:文件
<form action ="upload.php" method="post">
<label for="name">Image</label>
<input type="file" id="title" name="file" />
<input type="submit" value="Submit" name="submit" />
</form>
然后在upload.php
if (isset($_POST['submit'])) {
$file_name = $_FILES['image']['name'];
$file_type = $_FILES['image']['type'];
$file_size = $_FILES['image']['size'];
$file_tmp_name = $_FILES['image']['tmp_name'];
if($file_name){
move_uploaded_files(file_tmp_name,"uploads/$file_name");
}
}
将$ _POST ['submit']更改为$ _POST ['file']但是我得到了同样的错误。感谢
答案 0 :(得分:2)
首先在表单中添加enctype
<form action ="upload.php" method="post" enctype="multipart/form-data">
然后在您的PHP代码中使用名称而不是使用$ _FILES的图像。
if (isset($_POST['submit'])) {
$file_name = $_FILES['file']['name'];
$file_type = $_FILES['file']['type'];
$file_size = $_FILES['file']['size'];
$file_tmp_name = $_FILES['file']['tmp_name'];
if($file_name){
move_uploaded_files(file_tmp_name,"uploads/$file_name");
}
}