文件输入类型的未定义索引

a5g8bdjr  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(286)

这个问题在这里已经有答案了

使用php的“notice:未定义变量”、“notice:未定义索引”和“notice:未定义偏移量”(28个答案)
mysqli_fetch_assoc()需要参数/调用成员函数bind_param()时出错。如何获得实际的mysql错误并修复它(1个答案)
两年前关门了。
我有一个名为file的输入文件,但在提交表单时,它会返回:
未定义索引:第26行的c:\xampp\htdocs\rehab\image\u upload\index.php中的文件。
第26行是指: $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>

下面是我的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);
}
}
643ylb08

643ylb081#

你需要加上 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">

相关问题