I have an input file named file, but upon submitting the form, it returns:
Undefined index: file in C:\xampp\htdocs\Rehab\image_upload\index.php on line 26.
Line 26 refers to this:
$file = $_FILES['file'];
here's my 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>
and here's my php code:
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);
}
}