Can anyone help me I am facing problem while uploading image.
PHP
<?php
$msg="";
$db=mysqli_connect("localhost","root","root","man");
if(isset($_POST['submit'])) {
$fileName=$_FILES['uploadfile']['name'];
$fileTmpName=$_FILES['uploadfile']['tmp_name'];
$folder='images/';
move_uploaded_file($fileTmpName,$folder.$fileName);
$sql=mysqli_query($db,"INSERT INTO manimages(image) VALUES ($fileName)");
}
?>
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Uploading Images</title>
</head>
<body>
<form action="" method="POST">
<input type="file" name="uploadfile">
<input type="submit" name="submit" value="upload image">
</form>
</body>
</html>
Error is Notice: Undefined index: uploadfile in C:\wamp64\www\ftest\index.php on line 5 and Notice: Undefined index: uploadfile in C:\wamp64\www\ftest\index.php on line 6
Your form
tag is incomplete, for uploading images it should look like:
<form action="" method="post" enctype="multipart/form-data">
Remember if you are using file upload control, you must set enctype
to multipart/form-data
To allow forms to upload files, you should specify the right enctype
attribute, by default it's application/x-www-form-urlencoded
which you should change it to multipart/form-data
.
Try this:
<form action="" method="POST" enctype="multipart/form-data">
Now you can access the PHP
superglobal variable $_FILES
, thus no undefined index error will be thrown.