Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

PHP Image upload , Saving link to Database complete tutorial

PHP Image upload , Saving link to Database complete tutorial

This blog is dedicated to all our visitors who asked me to publish a working tutorial on  image upload  using PHP and Saving its path to Mysql Table.

PHP Image upload , Saving link to Database

How to do that :
First we will create a file called install.php , it will create a database called
myimages and a table  named imgtables and also create a folder called gallery where we are going to store  images.

Create a file name install.php
——————————————————

<?php
/*
file name : Install.php
purpose : creating a database name myimages
and table imgtables
Create by : Sanjay Prasad
[email protected]
http://www.openplus.in
*/$db=mysqli_connect(“localhost”,”root”,””);

if($db->connect_errno){
echo “Error <br/>”.$sp->error;
}

$query=”create database if not exists myimages”;

if($db->query($query)){
echo “Created database myimages ..<br/>”;

$sp=mysqli_connect(“localhost”,”root”,””,”myimages”);
if($sp->connect_errno){
echo “Error <br/>”.$sp->error;
}

$tbquery=”create table if not exists imgtables(
id int unsigned auto_increment primary key,
imgurl varchar(255),
date varchar(100)

)engine=’InnoDB'”;

if($sp->query($tbquery)){
echo “Table imgtables created Successfully  ..<br/> “;
}else{
echo “Error <br/>”.$sp->error;
}

//creating directory gallery

if(mkdir(“gallery”,0777)){
echo “Folder gallery create successfully”;
}else{
echo “Error Creating Directory”;
}

}
else{
echo “Error <br/>”.$sp->error;
}

?>

Now We have to create a file name gallery.php, which will upload images to folder gallery and saved their name like flower.jpg on table filed imgurl.

gallery.php

<!DOCTYPE html>
<html lang=”en”>
<head>
<title>File Uploading</title>
<meta charset=”utf-8″ />
</head><body>
<?php
$sp=mysqli_connect(“localhost”,”root”,””,”myimages”);
if($sp->connect_errno){
echo “Error <br/>”.$sp->error;
}$path=”gallery/”;

if(isset($_POST[‘upload’]))
{

$path=$path.$_FILES[‘file_upload’][‘name’];

if(move_uploaded_file($_FILES[‘file_upload’][‘tmp_name’],$path))
{
echo ” “.basename($_FILES[‘file_upload’][‘name’]).” has been uploaded<br/>”;
echo ‘<img src=”gallery/’.$_FILES[‘file_upload’][‘name’].'” width=”48″ height=”48″/>’;
$img=$_FILES[‘file_upload’][‘name’];
$query=”insert into imgtables (imgurl,date) values(‘$img’,now())”;
if($sp->query($query)){
echo “<br/>Inserted to DB also”;
}else{
echo “Error <br/>”.$sp->error;
}

}
else
{
echo “There is an error,please retry or ckeck path”;
}
}
?>
<form action=”gallery.php” method=”post” enctype=”multipart/form-data”>
<table width=”384″ border=”1″ align=”center”>
<tr>
<td width=”108″>Select File</td>
<td width=”260″><label>
<input type=”file” name=”file_upload”>
</label></td>
</tr>
<tr>
<td><label>
<input type=”submit” name=”upload” value=”Upload File”>
</label></td>
<td>&nbsp;</td>
</tr>
</table>
</form>
</body></html>

 

5 1 vote
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

25 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Anonymous

Hey, thanks for this nice Script 🙂
I have one question: I'll rename the uploaded file in one step. So my uploaded file looks at the end like this:

1. I upload the picture: flower.jpg
2. Then the script shall rename it to this: $_SESSION['user_id'].flower.jpg

Can someone help me with this problem?

Anonymous

can i use row id instead of user id

Gopi Kishan

This is nice blog which provides online tutorials and video's of different courses like:PHP,HTML,CSS,SEO Tutorials.
Tutorial In Noida

Anonymous

thank you soo much. it worked like a charm. i was running around google for days and finally found this piece of elegant work!! Thank you

Anonymous

great tutorial…i have only one question,,,and that too a lame one…how can i retrieve these files which are stored in the database.like i am sending a text file to someone,that person can download my text file

josephbupe

Sanjay please, how can I add FamilyName etc to save along with the image?

संजय जी
थोड़ी सी जिज्ञासा है ।

1- डेटाबेस में स्टोर्ड पोस्ट्स की लिंक कैसे बनायें । मतलब की किसी पोस्ट के टाइटल को क्लिक करने पर वो पोस्ट खुल जाये ये कैसे किया जायेगा ।

2-Php/mysql में seo (सर्च इंजन ऑप्टिमाइज) लिंक कैसे बनायीं जाये ।

सरल और उचित समाधान बताने का कष्ट करें ।

Venkat Rami Reddy

Thanku sanjay… your article is so helpful

Anonymous

thank u so much..

Fully Help

Checkout Great beginning php tutorials Very clear and helpful for beginners.

Fully Help

Nice blog…Very useful information is providing by ur blog. Great beginning php tutorials Very clear and helpful for beginners.

Hi, this blog is really amazing and provide me answers to all my questions .This is really informative and I will for sure refer my friends the same.Website Designing Company | Website Design Company

Fully Help

Nice blog…Very useful information is providing by ur blog. Great beginning php tutorials Very clear and helpful for beginners.

Satish Halmare

Sir how to make downloadebale to our uploaded file for for user please tell me

Rafi

how to save image automatically from uploaded directory existing file name without database(flat file)

GAN KOK WEI

if wanna show the image,
like normal select from db?

Anonymous

Thanks for the great information in your blog PHP

Tanu Chauhan

Thanks for such an interesting article here. I was searching for something like that for quite a long time and at last I have found it here.Web Designing Course in Jalandhar

Parminder Pal

Great point you make there. good POST.. I like your perspective on this subject. straight bevel gears

Filipe Fonseca

Ty U are the best!