PHP MySQL based simple image uploading system

I tried to built this system from youtube videos and by searching net.

I am sharing the code and what I have learnt today 🙂

We will use two php file in an one directory

Then go to your phpmyadmin and create a table then create a database and create a table name ‘store’ or paste this sql query on the SQL box on your phpmyadmin

CREATE TABLE IF NOT EXISTS `store` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `image` blob NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;

then create one file with your PHP IDE or different text editor and paste this code name of the file will be:index.php

<html>
<head>
<title>Upload an Image</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image"><input type="submit" value="Upload">
</form>
<?php
//connect to database
include 'get.php';

//file properties

if(!isset($_FILES['image'])){
echo "Please select an image.";
}else{
$image=addslashes(file_get_contents($_FILES['image']['tmp_name']));
echo $image_name=addslashes($_FILES['image']['name']);
$image_size=getimagesize($_FILES['image']['tmp_name']);
if($image_size==FALSE){
echo "That's not an image";
}else{
if(!$insert=mysqli_query($connect,"INSERT into store VALUES('','$image_name','$image')")){
echo "</br>Problem uploading image";
}
else
{
$lastid=mysqli_insert_id($connect);
echo "</br>Succsess !! Image Uploaded :)<p>Your image:</p><img src=get.php?id=$lastid>";
}
}
}
?>

</body>
</html>

Read out the whole code line by line and you will be definitely understand what I have written.

and second just create a get.php file and paste the code

<?php
$connect=mysqli_connect("localhost","your_db_username","your_db_password");
mysqli_select_db($connect,"your_dbname");
if(!$connect)
{
die("Connection error: " . mysqli_connect_error());
}


$id=addslashes($_REQUEST['id']);

$image=mysqli_query($connect,"SELECT * FROM store WHERE id=$id");
$image=mysqli_fetch_assoc($image);
$image=$image['image'];
header("Content-type:image/jpeg");
echo $image;

?>

Don’t forget to give your database name ,table name and password for your database username and then if you can connect successfully

Voila ! you are done ! 🙂

You can checkout this whole project in my github  I made it public and opensource you can also fork it and contribute.

Github : http://github.com/zakilive/phpimageupload

Please keep a comment if it’s helped you  or you have found any bug.Thanks for reading 🙂

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.