Saturday, 16 February 2019

how to upload image in php

<html>
<head>
<title>insert data</title>
<body>
<form action="" method="POST" enctype="multipart/form-data">
Name<input type="text" name="name"><br><br>
Age<input type="text" name="age"><br><br>
Contact No.<input type="text" name="contact"><br><br>
Upload Img<input type="file" name="image"><br><br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</head>
</html>
<?php

    $name=$_POST['name'];
$age=$_POST['age'];
$contact=$_POST['contact'];

if(isset($_POST['submit']))
{
//this code use for upload images
$fnm=$_FILES["image"]["name"];
    $dst="./images/".$fnm;
    move_uploaded_file($_FILES["image"]["tmp_name"],$dst);
if($name!=""&& $age!="" && $contact!="" && $fnm!="")
{
$query="INSERT INTO stud(name,age,contact,image)VALUES('$name',$age,$contact,'$dst')";
$data=mysqli_query($con, $query);

if($data)
{
echo"dta submited";
}
}
else
{
echo "all field are required";
}
}
?>

6 comments: