Saturday, February 1, 2014

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
sonzoy@gmail.com
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>


PickMe
Delicious Save this on Delicious

20 comments :

  1. 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?

    ReplyDelete
    Replies
    1. change
      $path=$path.$_FILES['file_upload']['name'];
      to
      $path=$path. $_SESSION['user_id'].$_FILES['file_upload']['name'];

      Delete
    2. can i use row id instead of user id

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

    ReplyDelete
  3. 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

    ReplyDelete
  4. 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

    ReplyDelete
    Replies
    1. we are not strong image,we storing their path, we can retrieve like
      note 1 is row id ...next
      $query=$sp->query("select * from imgtables where id='1') or die($sp->error);

      $rows=$query->fetch_assoc();

      echo $rows['imgurl'];

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

    ReplyDelete
    Replies
    1. better download my project Adminplus http://www.openplus.in/open-articles/view-story-project-adminplus.htm
      you will learn making admin panel uploading picture and so on

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

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

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

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

    ReplyDelete
    Replies
    1. Anand ji ,
      ek table create karte post ,columns hai ID, TITLE, DESCRIPTION .....
      ID auto_increment hoga, matlab jab ham ek post create karte hai to ek id generate hoti hai man lijiye 5 , jab dusara create karenge tab 6 hoga ...

      ek page create karte hai read.php ....
      jab hum read.php?id=5 pe jaate hai tab $_GET['id']; se 5 collect karte hai aur query karte hai
      select * from post where id='5'
      aur hume sari details prapt hoti hai

      Live example - http://www.openplus.in/jokes/read.php?id=2108

      SEO URL
      read.php?id=5 ko hum url rewritte tool ki madad se seo friendly url prapt karte hai

      Live example - http://www.openplus.in/open-articles/view-story-samsung-z1-tizen-based-os-review.htm

      Delete
  7. Thanku sanjay... your article is so helpful

    ReplyDelete
  8. thank u so much..

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

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

    ReplyDelete
  11. 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

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

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

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

    ReplyDelete

If you have any Question , Suggestion or idea then please comment regarding the post.