Php : Image uploading and retrieving image from database

Upload image into the database and retrieve from it

See this in video:   Image upload
Create the database name as 'image' and just copy this table given below
Database
CREATE TABLE IF NOT EXISTS `upload` ( `id` int(255) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `image` longblob NOT NULL, PRIMARY KEY (`id`) )

Html:

PHP: (upload and display)
mysql_connect("localhost","root",""); mysql_select_db("image"); if(isset($_POST["submit"])){ $filename = addslashes($_FILES['img']['name']); $tmpname = addslashes(file_get_contents($_FILES['img']['tmp_name'])); $filetype = addslashes($_FILES['img']['type']); $filesize = addslashes($_FILES['img']['size']); $array = array('jpg','jpeg'); $ext = pathinfo($filename, PATHINFO_EXTENSION); if(!empty($filename)){ if(in_array($ext, $array)){ mysql_query("Insert into upl(name,image) values('$filename','$tmpname')"); echo "uploaded"; } else{ echo "failed"; } } } //display $res = mysql_query("SELECT * FROM upl"); while($row = mysql_fetch_array($res)){ $displ = $row['image']; // please place the&nbsp;single quotation ' instead ' echo '<img src="data:image/jpeg;base64,&#39;.base64_encode($displ).&#39;" />'; echo "<br />"; }

Comments

Post a Comment

Popular posts from this blog

PHP Login and Registration script

Multiple image upload to database and display from it

Java program to display diamond pattern