未定义的索引虽然已定义

时间:2018-04-14 11:16:08

标签: php html mysql file-upload

所以这是我的问题,我已经定义了我的输入文件,甚至将PHP代码放在文件本身,但同样的问题也出现了:

  

未定义的索引:第26行的C:\ xampp \ htdocs \ Rehab \ image_upload \ index.php中的文件。

这是我的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'];
$error = $file['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)){
        $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);

        var_dump($query);
    }
    else{
        echo "<script>";
         echo "alert('Invalid file type!');";
        echo "</script>";
    }
}
else{
    echo "<script>";
     echo "alert('Error! File is too large!');";
    echo "</script>";
    }
}

1 个答案:

答案 0 :(得分:0)

您需要在表单标记中添加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">