Home >Backend Development >PHP Tutorial >PHP Tutorial: How to Use BLOB to Access Image Information Example_PHP Tutorial
BLOB is a MySQL data type, called a binary large object. As its name suggests, it is used to store large amounts of string data similar to MYSQL binary and VARBINARY types.
MySQL BLOB classification
LONGBLOB ((2 ^ 32)1)
CREATE TABLE IF NOT EXISTS `output_images` ( `imageId` tinyint(3) NOT NULL AUTO_INCREMENT, `imageType` varchar(25) NOT NULL DEFAULT '', `imageData` mediumblob NOT NULL, PRIMARY KEY (`imageId`) )
<?php if(count($_FILES) > 0) { if(is_uploaded_file($_FILES['userImage']['tmp_name'])) { mysql_connect("localhost", "root", ""); mysql_select_db ("phppot_examples"); $imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name'])); $imageProperties = getimageSize($_FILES['userImage']['tmp_name']); $sql = "INSERT INTO output_images(imageType ,imageData) VALUES('{$imageProperties['mime']}', '{$imgData}')"; $current_id = mysql_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysql_error()); if(isset($current_id)) { header("Location: listImages.php"); }}} ?> <HTML> <HEAD> <TITLE>Upload Image to MySQL BLOB</TITLE> <link href="imageStyles.css" rel="stylesheet" type="text/css" /> </HEAD> <BODY> </div> </BODY> </HTML>
<?php $conn = mysql_connect("localhost", "root", ""); mysql_select_db("phppot_examples") or die(mysql_error()); if(isset($_GET['image_id'])) { $sql = "SELECT imageType,imageData FROM output_images WHERE imageId=" . $_GET['image_id']; $result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error()); $row = mysql_fetch_array($result); header("Content-type: " . $row["imageType"]); echo $row["imageData"]; } mysql_close($conn); ?>
<img src="http://blog.csdn.net/u012275531/article/details/imageView.php?image_id=<?php echo $row["imageId"]; ? alt="PHP Tutorial: How to Use BLOB to Access Image Information Example_PHP Tutorial" >" />
List BLOB Images <img src="http://blog.csdn.net/u012275531/article/details/imageView.php?image_id=<?php echo $row["imageId"]; ? alt="PHP Tutorial: How to Use BLOB to Access Image Information Example_PHP Tutorial" >" />