<?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>Prabeen&#039;s Blog &#187; MSSQL Server</title>
	<atom:link href="http://prabeengiri.com.np/category/mssql-server/feed" rel="self" type="application/rss+xml" />
	<link>http://prabeengiri.com.np</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 04 Jan 2011 18:31:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Storing and displaying the image stored as varbinary in  MSSQL server using PHP (CodeIgniter)</title>
		<link>http://prabeengiri.com.np/php/stroring-and-displaying-the-image-stored-as-varbinary-in-mssql-server-using-php-codeigniter.html</link>
		<comments>http://prabeengiri.com.np/php/stroring-and-displaying-the-image-stored-as-varbinary-in-mssql-server-using-php-codeigniter.html#comments</comments>
		<pubDate>Mon, 05 Jul 2010 13:11:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[MSSQL Server]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[image]]></category>

		<guid isPermaLink="false">http://prabeengiri.com.np/?p=593</guid>
		<description><![CDATA[Storing and displaying the image stored as varbinary in  MSSQL server using PHP (CodeIgniter), display files in browser , MSSQL server datatype varbinary(MAX), msssql server and codeigniter display images.   ]]></description>
			<content:encoded><![CDATA[<p>I am not used to MSSql server with php . MSSQL server has very less documentation compared to Mysql . So here I am writing a short snippets to upload image to MSSQL server in field type &#8216;varbinary&#8217; and display the image to browser using php framework <a href="http://codeigniter.com/">codeIgniter</a>. Using framework for php application development is always a good idea.<span id="more-593"></span></p>
<p>Here is a php snippet  to  store the uploaded image in the MSSQL server ( datatype = varbinay(MAX)) .</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> do_upload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'upload_path'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'./uploads/'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'allowed_types'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'gif|jpg|png'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'max_size'</span><span style="color: #009900;">&#93;</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">'100'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'max_width'</span><span style="color: #009900;">&#93;</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">'1024'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'max_height'</span><span style="color: #009900;">&#93;</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">'768'</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">library</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'upload'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">upload</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">do_upload</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">upload</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">display_errors</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'upload_form'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$error</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>	
		<span style="color: #b1b100;">else</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">upload</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
&nbsp;
                        <span style="color: #000088;">$dataString</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'full_path'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #000088;">$arrData</span> <span style="color: #339933;">=</span> <span style="color: #990000;">unpack</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;H*hex&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dataString</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #000088;">$data_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;0x&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$arrData</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'hex'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                        <span style="color: #666666; font-style: italic;">// now we can insert the $data_string variable into the sql server using it respective sql query.</span>
			delete_files<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'full_path'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// we don't want the image to be stored in the web server. </span>
                        <span style="color: #666666; font-style: italic;">//$this-&gt;load-&gt;view('upload_success', $data);</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now its little tricky to render the image in the browser that is stored in the MSSQL server ,varbinary(MAX) datatype.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;img src=&quot;<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'BASEURL'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>display_image/convert_image/$row-&gt;id?&gt; width=&quot;74&quot; height=&quot;53&quot; /&gt;
&nbsp;
// baseurl returns the base_url().'index.php/'  (my custom config) 
// display_image is controller name  
// convert_image is method name 
//$row-&gt;id returns the primary key of that particular image row.It is used for query to the database.</pre></div></div>

<p>Now here is my controller to render image on the browser .</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'BASEPATH'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No direct script access allowed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Display_Image <span style="color: #000000; font-weight: bold;">extends</span> Controller
<span style="color: #009900;">&#123;</span> 
	<span style="color: #000000; font-weight: bold;">function</span> Display_Image<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span> 
		parent<span style="color: #339933;">::</span><span style="color: #004000;">Controller</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #009900;">&#125;</span> 
	<span style="color: #000000; font-weight: bold;">function</span> convert_image<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span> 
&nbsp;
		<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span>  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">my_model</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">uri</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">segment</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// to get the image data from the database where my_model is model to fetch the row. </span>
                <span style="color: #000088;">$image_data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">image</span><span style="color: #339933;">;</span> 
		<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Type: image/jpeg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// to tell browser that content type is jpeg image.  </span>
		<span style="color: #b1b100;">print</span> <span style="color: #000088;">$image_data</span><span style="color: #339933;">;</span> 
&nbsp;
	<span style="color: #009900;">&#125;</span> 
&nbsp;
<span style="color: #009900;">&#125;</span> 
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This is how we can store and display image using MSSQL server in datatype varbinary (MAX).<br />
I hope this information is helpful who are having hard time to find the solution . </p>
]]></content:encoded>
			<wfw:commentRss>http://prabeengiri.com.np/php/stroring-and-displaying-the-image-stored-as-varbinary-in-mssql-server-using-php-codeigniter.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

