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

3 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. This is nice blog which provides online tutorials and video's of different courses like:PHP,HTML,CSS,SEO Tutorials.
    Tutorial In Noida

    ReplyDelete

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