PHP to the image in binary mysql database and display the implementation code

  • 2020-03-31 20:49:06
  • OfStack

// save the image to the database PHP code
 
If($Picture != "none") { 
$PSize = filesize($Picture); 
$mysqlPicture = addslashes(fread(fopen($Picture, "r"), $PSize)); 
mysql_connect($host,$username,$password) or die("Unable to connect to SQL server"); 
@mysql_select_db($db) or die("Unable to select database"); 
mysql_query("INSERT INTO Images (Image) VALUES ($mysqlPicture)") or die("Cant Perform Query"); 
}else { 
echo"You did not upload any picture"; 
} 

// the code to read the images in the database with the img tag
 
mysql_connect($host,$username,$password) or die("Unable to connect to SQL server"); 
@mysql_select_db($db) or die("Unable to select database"); 
$result=mysql_query("SELECT * FROM Images") or die("Cant Perform Query"); 
While($row=mysql_fetch_object($result)) { 
echo "<IMG SRC="Second.php3? PicNum=$row->PicNum">"; 

//The code for the secoed. PHP file is as follows
$result=mysql_query("SELECT * FROM Images WHERE PicNum=$PicNum") or die("Cant perform Query"); 
$row=mysql_fetch_object($result); 
Header( "Content-type: image/gif"); 
echo $row->Image; 

Related articles: