<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rayhan's blog (raynux.com) &#187; Image</title>
	<atom:link href="http://raynux.com/blog/tag/image/feed/" rel="self" type="application/rss+xml" />
	<link>http://raynux.com/blog</link>
	<description>Rayhan's Personal Web Blog Site</description>
	<lastBuildDate>Mon, 11 Apr 2011 06:33:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Store and Display image from MySQL database</title>
		<link>http://raynux.com/blog/2008/11/20/store-and-display-image-from-mysql-database/</link>
		<comments>http://raynux.com/blog/2008/11/20/store-and-display-image-from-mysql-database/#comments</comments>
		<pubDate>Thu, 20 Nov 2008 10:09:13 +0000</pubDate>
		<dc:creator>rayhan</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Reference]]></category>
		<category><![CDATA[Image]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Upload]]></category>

		<guid isPermaLink="false">http://raynux.com/blog/?p=47</guid>
		<description><![CDATA[A basic approach to upload and save image to a MySQL database and then display the image from the database. First you need to create a table in MySQL Database to store the image data. Log into you database and run the following sql command: CREATE TABLE  `images` ( `id` int(11) NOT NULL auto_increment, `name` [...]]]></description>
			<content:encoded><![CDATA[<p>A basic approach to upload and save image to a MySQL database and then display the image from the database.</p>
<p>First you need to create a table in MySQL Database to store the image data. Log into you database and run the following sql command:<br />
<code>CREATE TABLE  `images` (<br />
`id` int(11) NOT NULL auto_increment,<br />
`name` varchar(100) default NULL,<br />
`size` int(11) default NULL,<br />
`type` varchar(20) default NULL,<br />
`content` mediumblob,<br />
PRIMARY KEY  (`id`)<br />
) ENGINE=MyISAM;</code></p>
<p>Once the table have been created, we are ready to write codes. Now create a folder in you webserver named <strong>rayImg</strong> and create image.php and upload.php files inside the folder.</p>
<p>Open the <strong>image.php</strong>, copy and paste the following code and save it.</p>
<p>File: image.php</p>
<pre><code>&lt;?php
	/**
	 * Display image form database
	 *
	 * Retrive an image from mysql database if image id is provided.
	 *
	 * @example to display a image with image id 1, place &lt;img src="image.php?id=1" &gt; in your html file.
	 *
	 * @author Md. Rayhan Chowdhury
	 * @copyright www.raynux.com
	 * @license LGPL
	 */

	// verify request id.
	if (empty($_GET['id']) || !is_numeric($_GET['id'])) {
		echo 'A valid image file id is required to display the image file.';
		exit;
	}

	$imageId = $_GET['id'];

	//connect to mysql database
	if ($conn = mysqli_connect('localhost', 'root', 'root', 'test')) {
		$content = mysqli_real_escape_string($conn, $content);
		$sql = "SELECT type, content FROM images where id = {$imageId}";

		if ($rs = mysqli_query($conn, $sql)) {
			$imageData = mysqli_fetch_array($rs, MYSQLI_ASSOC);
			mysqli_free_result($rs);
		} else {
			echo "Error: Could not get data from mysql database. Please try again.";
		}
		//close mysqli connection
		mysqli_close($conn);

	} else {
		echo "Error: Could not connect to mysql database. Please try again.";
	}	

	if (!empty($imageData)) {
		// show the image.
		header("Content-type: {$imageData['type']}");
		echo $imageData['content'];
	}
?&gt;</code></pre>
<p>Now open the update.php, copy and paste the following code and save it.</p>
<p>File: upload.php</p>
<pre><code>&lt;?php
/**
 * Upload an image to mysql database.
 *
 *
 *
 * @author Md. Rayhan Chowdhury
 * @copyright www.raynux.com
 * @license LGPL
 */

// Check for post data.
if ($_POST &amp;&amp; !empty($_FILES)) {
	$formOk = true;

	//Assign Variables
	$path = $_FILES['image']['tmp_name'];
	$name = $_FILES['image']['name'];
	$size = $_FILES['image']['size'];
	$type = $_FILES['image']['type'];

	if ($_FILES['image']['error'] || !is_uploaded_file($path)) {
		$formOk = false;
		echo "Error: Error in uploading file. Please try again.";
	}

	//check file extension
	if ($formOk &amp;&amp; !in_array($type, array('image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/gif'))) {
		$formOk = false;
		echo "Error: Unsupported file extension. Supported extensions are JPG / PNG.";
	}
	// check for file size.
	if ($formOk &amp;&amp; filesize($path) &gt; 500000) {
		$formOk = false;
		echo "Error: File size must be less than 500 KB.";
	}

	if ($formOk) {
		// read file contents
		$content = file_get_contents($path);

		//connect to mysql database
		if ($conn = mysqli_connect('localhost', 'root', 'root', 'test')) {
			$content = mysqli_real_escape_string($conn, $content);
			$sql = "insert into images (name, size, type, content) values ('{$name}', '{$size}', '{$type}', '{$content}')";

			if (mysqli_query($conn, $sql)) {
				$uploadOk = true;
				$imageId = mysqli_insert_id($conn);
			} else {
				echo "Error: Could not save the data to mysql database. Please try again.";
			}

			mysqli_close($conn);
		} else {
			echo "Error: Could not connect to mysql database. Please try again.";
		}
	}
}
?&gt;

&lt;html&gt;
	&lt;head&gt;
		&lt;title&gt;Upload image to mysql database.&lt;/title&gt;
		&lt;style type="text/css"&gt;
			img{
				margin: .2em;
				border: 1px solid #555;
				padding: .2em;
				vertical-align: top;
			}
		&lt;/style&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;?php if (!empty($uploadOk)): ?&gt;
			&lt;div&gt;
		  		&lt;h3&gt;Image Uploaded:&lt;/h3&gt;
		  	&lt;/div&gt;
			&lt;div&gt;
				&lt;img src="image.php?id=&lt;?=$imageId ?&gt;" width="150px"&gt;
				&lt;strong&gt;Embed&lt;/strong&gt;: &lt;input size="25" value='&lt;img src="image.php?id=&lt;?=$imageId ?&gt;"&gt;'&gt;
			&lt;/div&gt;

			&lt;hr&gt;
		&lt;? endif; ?&gt;

		&lt;form action="&lt;?=$_SERVER['PHP_SELF']?&gt;" method="post" enctype="multipart/form-data" &gt;
		  &lt;div&gt;
		  	&lt;h3&gt;Image Upload:&lt;/h3&gt;
		  &lt;/div&gt;
		  &lt;div&gt;
		  	&lt;label&gt;Image&lt;/label&gt;
		  	&lt;input type="hidden" name="MAX_FILE_SIZE" value="500000"&gt;
			&lt;input type="file" name="image" /&gt;
		    &lt;input name="submit" type="submit" value="Upload"&gt;
		  &lt;/div&gt;
		&lt;/form&gt;
	&lt;/body&gt;
&lt;/html&gt;</code></pre>
<p>Replace line</p>
<p><strong>mysqli_connect(&#8216;localhost&#8217;, &#8216;root&#8217;, &#8216;root&#8217;, &#8216;test&#8217;) </strong></p>
<p>with<strong></strong></p>
<p><strong>mysqli_connect(&#8216;your host&#8217;, &#8216;your username&#8217;, &#8216;your password&#8217;, &#8216;your database name&#8217;)</strong></p>
<p>in both the file and save again.</p>
<p>You are done. Open upload.php from your browser and upload you desired image and view the uploaded image.</p>
]]></content:encoded>
			<wfw:commentRss>http://raynux.com/blog/2008/11/20/store-and-display-image-from-mysql-database/feed/</wfw:commentRss>
		<slash:comments>69</slash:comments>
		</item>
	</channel>
</rss>

