我有一个名为 文件 ,但在提交表单时,它会返回:
未定义的索引:文件位于C:\xampp\htdocs\Rehab\image\u upload\index。php第26行。
第26行 指的是: $file = $_FILES['file'];
$file = $_FILES['file'];
这是我的html:
<form class="container-fluid d-flex justify-content-between" action="index.php" method="POST"> <div class="col-md-4 mt-5 border-right pr-5 mr-5"> <div class="form-group text-center"> <legend class="display-4">Image Upload</legend> </div> <div class="form-group"> <label for="title">Title</label> <input type="text" class="form-control" name="title" id="title" autocomplete="off"> </div> <div class="form-group"> <label for="image">Image</label> <input type="file" class="form-control" name="file" id="name"> </div> <div class="form-group text-center"> <button class="btn btn-outline-primary" name="submit">Post</button> </div> </div> <div class="col-md"></div> </form>
下面是我的php代码:
if(isset($_POST['submit'])){ $file = $_FILES['file']; if ($file['error'] > 0) { return echo "Error!"; } if($error === 0){ $name = $file['name']; $type = $file['type']; $size = $file['size']; $tmp_name = $file['tmp_name']; $name_explode = explode('.', $name); $file_ext = strtolower($name_explode[1]); $allowed = array('jpg','jpeg','png','gif'); if(!in_array($file_ext, $allowed)) { return echo "Invalid file type!"; } $new_name = uniqid('', true) . "." . $file_ext; $location = 'assets/images/' . $new_name; $query = $conn->query("INSERT INTO medicine VALUES ()"); $move = move_uploaded_file($tmp_name, $location); } }
您需要添加 enctype="multipart/form-data" 在表单标签中。因为 enctype 属性用于 <input type="file"> 您的标签应该是
enctype="multipart/form-data"
enctype
<input type="file">
<form class="container-fluid d-flex justify-content-between" action="index.php" method="POST" enctype="multipart/form-data">