requirements:
- a database to store the image. here we used users named database
- a folder where we upload the image.we used userdata named table.
<html>
<head>
</head>
<body>
<FORM method="post" enctype="multipart/form-data">
<input type="file" name="file1" id="file" onchange="loadFile(event)" style="display: none;">
<label for="file" style="cursor: pointer;background:rgba(5,4,3,0.3);padding:5px;border:2px outset black;" >Upload Image</label>
<input type="submit" name="upld">
</form>
<div style="background:rgba(5,4,3,0.3);height:100px;width:100px;">
<img id="output" width="200" style="width:100%;height:100%;border:2px solid red;">
</div>
</body>
</html>
<?php
if (isset($_POST['upld']))
{
$fn = $_FILES["file1"]["name"];
$tmp_n = $_FILES["file1"]["tmp_name"];
$folder = "./photo/" . $fn;
$conn = new mysqli("localhost", "root", "", "user");
$sql = "INSERT INTO userdata (image) VALUES ('$fn')";
// Execute query
$q1=$conn->query($sql);
if (move_uploaded_file($tmp_n, $folder))
{
echo "<h3> Image uploaded successfully</h3>";
}
else
{
echo "<h3> Failed to upload image!</h3>";
}
}
?>
<script>
var loadFile = function(event) {
var image = document.getElementById('output');
image.src = URL.createObjectURL(event.target.files[0]);
};
</script>
0 Comments