How to store image in database in Php
In this example, we are learning to how to store image in database using Php.
First of all, create a folder of images name where your code file available. After create a database with two column of id and name.
We are storing just path of the image and give id of the image in database.
Let's see the example that how it works :
file name : index.php
file name : image_upload.php
Output :
Note :
database name : demo
table name : img
field name : id (varchar) , name (varchar)
First of all, create a folder of images name where your code file available. After create a database with two column of id and name.
We are storing just path of the image and give id of the image in database.
Let's see the example that how it works :
file name : index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 | <HTML> <BODY> <FORM METHOD="POST" ACTION="image_upload.PHP" enctype="multipart/form-data"> <pre> Example of Image Upload<br> ID : <INPUT TYPE='TEXT' NAME='ID'> <br/> Image:<input name="uploadedfile" type="file" /> <br/> <INPUT TYPE="SUBMIT" VALUE="Insert"> <INPUT TYPE="RESET" VALUE="CLEAR"> </pre> </FORM> </BODY> </HTML> |
file name : image_upload.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <?php $id=$_POST['ID']; $conn=mysql_connect("localhost","root",""); mysql_select_db("demo"); $fn=basename($_FILES['uploadedfile']['name']); $target_path = "./images/"; $target_path = $target_path.$fn; $tfn=$_FILES['uploadedfile']['tmp_name']; $x=move_uploaded_file($tfn,$target_path); if($x) { echo "<img src='images/".basename($_FILES['uploadedfile']['name'])."'</img> "; echo "<br>The file <u>[".basename($_FILES['uploadedfile']['name'])."</u>] has been uploaded<br>"; } else { echo "There was an error uploading the file, please try again!"; die; } $insert="insert into img values($id,'$fn')"; mysql_query($insert) or die("INSERT ".mysql_error()); mysql_query("commit") or die("commit fail ".mysql_error()); echo "<br>Record Successfully Added"; ?> <a href="index.php">Back to Home</a> |
Output :
Note :
database name : demo
table name : img
field name : id (varchar) , name (varchar)
file upload control in php, How to store image in database in php, how to store image path in database in php, image store in database in php
0 comments:
Post a Comment