Created
March 9, 2017 08:27
-
-
Save Mauryashubham/f60e0f7c1b7b1a54bdf0095753708c90 to your computer and use it in GitHub Desktop.
Upload and Retrieve Image From Database in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hi all , Welcome to Maurya Tricks , Today we are going to discuss , | |
How to Upload and Retrieve Image From Database in PHP | |
//Database Creation | |
Make a database named “testdb and table named “image” . | |
Give the element name 'b_image' (varchar(255)) in tables 'test'. | |
//Make a folder named "image" in your directory , here the images get saved | |
1.Make a file in notepad and save it as index.php and paste the below code. | |
<?php | |
/** | |
@author : Shubham Maurya, | |
Email id : [email protected] | |
**/ | |
//connect to mysql database | |
$con = mysqli_connect("localhost", "root", "", "testdb") | |
or die("Error " . mysqli_error($con)); | |
//Upload Image | |
if(isset($_POST['cover_up'])) | |
{ | |
$imgFile = $_FILES['coverimg']['name']; | |
$tmp_dir = $_FILES['coverimg']['tmp_name']; | |
$imgSize = $_FILES['coverimg']['size']; | |
if(!empty($imgFile)) | |
{ | |
$upload_dir = 'image/'; // upload directory | |
$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension | |
// valid image extensions | |
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions | |
// rename uploading image | |
$coverpic = rand(1000,1000000).".".$imgExt; | |
// allow valid image file formats | |
if(in_array($imgExt, $valid_extensions)){ | |
// Check file size '5MB' | |
if($imgSize < 5000000) { | |
move_uploaded_file($tmp_dir,$upload_dir.$coverpic); | |
} | |
else{ | |
$errMSG = "Sorry, your file is too large."; | |
} | |
} | |
else{ | |
$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; | |
} | |
//For Database Insertion | |
// if no error occured, continue .... | |
if(!isset($errMSG)) | |
{ | |
$que = "INSERT INTO image(b_image) VALUES('" . $coverpic . "')"; | |
if(mysqli_query($con, $que)) | |
{ | |
echo "<script type='text/javascript'>alert('Posted succesfully.');</script>"; | |
} | |
else | |
{ | |
echo "<script type='text/javascript'>alert('error while inserting....');</script>"; | |
} | |
} | |
} | |
} | |
//Get Last Inserted Id | |
$last_id = mysqli_insert_id($con); | |
//Fetch Qquery | |
$que = "SELECT * FROM image where id='$last_id' "; | |
$result = mysqli_query($con, $que); | |
$row=mysqli_fetch_assoc($result); | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
</head> | |
<body> | |
<form method="post" enctype="multipart/form-data"> | |
<p><input type="file" name="coverimg" required="required" /></p> | |
<p><input type="submit" name="cover_up" style="background-color: rgb(255, 102, 0);" class="btn btn-warning" value="Upload"/></p> | |
</form> | |
<!--Display Image--> | |
<div> | |
<label>Image : </label><br><img src="image/<?php echo $row['b_image']; ?> " alt="image"> | |
</div> | |
</body> | |
</html> |
Author
Mauryashubham
commented
May 3, 2020
via email
Please check the code once and try to solve the error once by yourself. :)
…ok thnks i am trying
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment