how to upload image into database using php


how to upload file in database in php

how to upload image into database using php

step 1

1. create database demo


CREATE TABLE IF NOT EXISTS `table1` (
  `id` int(11) NOT NULL auto_increment,
  `file` longblob NOT NULL COMMENT 'file in binary',
  `type` varchar(250) NOT NULL,
  `name` varchar(250) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


2. create two file 


demo.php

photo.php


demo.php

<?php 

$con
=mysql_connect('localhost','root','');
mysql_select_db('demo',$con);

//print_r($_FILES);
if(isset($_REQUEST['sub'])){
 
$tpy=$_FILES['txtfile']['type'];
  
$name=$_FILES['txtfile']['name'];
   
$c=file_get_contents($_FILES['txtfile']["tmp_name"]);
mysql_query("insert into table1(file,type,name) values('".addslashes($c)."', '".$tpy."', '".$name."')");
}
$arr=mysql_query("select * from table1");
while(
$r=mysql_fetch_array($arr)){
// echo "<img src='photo.php?id=".$r[0]."'  />";
 
echo "<a href='photo.php?id=".$r[0]."'>Download here </a>";
 }
?>

<form action="" enctype="multipart/form-data" method="post">
<input type="file" name="txtfile"  />
<input type="submit" name="sub" value="submit"  />
</form> 

photo.php

<?php 

$con
=mysql_connect('localhost','root','');
mysql_select_db('demo',$con);

$arr=mysql_query("select * from table1 where id= '".$_REQUEST['id']."' ");
while(
$r=mysql_fetch_array($arr)){
header("Content-Disposition: attachment;filename=".$r['name']."");
$ty=$r[2];
header("Content-type:".$ty."");
echo 
$r[1];



}
?>

Comments

Popular posts from this blog

Call PHP Function In JavaScript Using Ajax

List of Post Category Wise in wordpress