form with image upload php code example
Example 1: php image upload in database
<?php
error_reporting(0);
?>
<?php
$msg = "";
if (isset($_POST['upload'])) {
$filename = $_FILES["uploadfile"]["name"];
$tempname = $_FILES["uploadfile"]["tmp_name"];
$folder = "image/".$filename;
$db = mysqli_connect("localhost", "root", "", "image_upload");
$sql = "INSERT INTO images (filename) VALUES ('$filename')";
mysqli_query($db, $sql);
if (move_uploaded_file($tempname, $folder)) {
$msg = "Image uploaded successfully";
}else{
$msg = "Failed to upload image";
}
}
$result = mysqli_query($db, "SELECT * FROM images");
?>
<!DOCTYPE html>
<html>
<head>
<title>Image Upload</title>
<link rel="stylesheet" type= "text/css" href ="style.css"/>
<div id="content">
<form method="POST" action="" enctype="multipart/form-data">
<input type="file" name="uploadfile" value=""/>
<div>
<button type="submit" name="upload">UPLOAD</button>
</div>
</form>
</div>
</body>
</html>
Example 2: how to upload image in php
<!DOCTYPE html><html><body><form action="upload.php" method="post"
enctype="multipart/form-data"> Select image to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload Image" name="submit">
</form></body></html>