How to get image from url and insert into database and store image in your local root folder through PHP and mysql?

Description:- Hi ! In this tutorial I’m going to provide you code to get the image from url and then to store in the database as well in your local root directory or in a folder.
You can also include this type of image search thing in your website so that user will get limitless option to upload a picture.
I use rand() function to rename the image name and it will be helpful for you when you make a large website. 

This chapter will complete in 3 different steps:-
1. Make index.php
2. Make a new folder in which your image will store and rename that folder as ‘image’
3. Make a new database and give it a name mukulexp

1. index.php

<?php
// connection to database
$con = mysql_connect("localhost","root","");
mysql_select_db('mukulexp',$con) or die("could not connect");
?>
<?php
if(isset($_REQUEST['subimg']))
{
 // Your file
 $file = $_REQUEST['url'];
 
// Open the file to get existing content
$data = file_get_contents($file);
$newimage=rand()."abc.jpg";
// New file
$new = 'image/'.$newimage;
// Write the contents back to a new file
file_put_contents($new, $data);
mysql_query("Insert into image set name = '$newimage' ");
 echo "<img src='image/$newimage' width='300px' height='250px'/>";
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="url" name="url"/><br/><br/>
<input type="submit" name="subimg" value="submit"/> 
</form>

2. Make a new folder and rename it as image

3. Make a new database and give it a name of mukulexp and then enter in your mukulexp and click on SQL and paste the below code or make the table by yourself and name it image and then make 2 columns in it first id and second name

CREATE TABLE IF NOT EXISTS `image` (
 `id` int(90) NOT NULL AUTO_INCREMENT,
 `name` varchar(4000) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
--
-- Dumping data for table `image`
--
INSERT INTO `image` (`id`, `name`)

NOTE : – Do all the above point as they are mentioned. Watch video for help
Happy Coding 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s