<?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>0t@k-B@L! &#187; database</title>
	<atom:link href="http://www.otakbali.com/tag/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.otakbali.com</link>
	<description>think about web programming</description>
	<lastBuildDate>Mon, 02 Aug 2010 03:56:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Storing Images/Binary Files to mySQL in PHP</title>
		<link>http://www.otakbali.com/storing-imagesbinary-files-to-mysql-in-php.html</link>
		<comments>http://www.otakbali.com/storing-imagesbinary-files-to-mysql-in-php.html#comments</comments>
		<pubDate>Tue, 10 Nov 2009 13:58:06 +0000</pubDate>
		<dc:creator>andi</dc:creator>
				<category><![CDATA[Tips n Trik]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[trick]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://otakbali.com/?p=59</guid>
		<description><![CDATA[how to store images, audio, or another binary files into mySQL database with PHP script. cara menyimpan gambar ke mysql dengan PHP.]]></description>
			<content:encoded><![CDATA[<p>It is possible for us to store/save our images (binary files) into a mySQL database in PHP. In this article, we will discuss about &#8220;how to store images, audio, or another binary files into mySQL database with PHP script&#8221;. But, before we continue to the further step, you might want to consider some reason of why we should and why we shouldn&#8217;t store binary files in a database:</p>
<p>Reasons to store your images (binary files) in a database:</p>
<ol style="list-style-type: decimal;">
<li>Storing in a database allows better security for your files and images.</li>
<li>Eliminates messy directory and file structures (this is especially true when users are allow to contribute files).</li>
<li>Files/Images can be stored directly linking to user or advertiser information.<span id="more-59"></span></li>
</ol>
<p>You may want to reconsider storing your binary files in the database.  Here are some reason you may not want to:</p>
<ol style="list-style-type: decimal;">
<li>You can&#8217;t directly access your files using standard applications such as FTP.</li>
<li>If you database becomes corrupt, so do your files.</li>
<li>Migrating to a new database is made more difficult</li>
<li>Pulling images from a database and displaying them is slower than using file access.</li>
</ol>
<p>After considering above reasons, you may continue to the first step of this tutorial.</p>
<p><span style="font-size: medium;">Database Type and Connection</span></p>
<p>I assume that you already create a mySQL database named &#8220;binary&#8221;. We created two tables, one the primary ID (of the row/entry) and the binary BLOB. A BLOB is a binary large object that can hold a variable amount of data. The four BLOB types are TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB. These differ only in the maximum length of the values they can hold. The four TEXT types are TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT. These correspond to the four BLOB types and have the same maximum lengths and storage requirements.</p>
<p>You&#8217;ve now created your database and are ready to upload binary files to it. In this case, images. Next we will create a simple upload script.</p>
<p><span style="font-size: medium;">Creating the HTML</span></p>
<p>Creating the user interface where you or your visitors will upload files is very simple.  Using basic HTML and relying on the browser to do most of the heavy lifting you can easily create an upload form.  Here is the one we will use:</p>
<div style="margin: 5px 20px 20px;">
<div class="smallfont" style="margin-bottom: 2px;">HTML Code:</div>
<pre class="alt2" style="border: 1px inset; margin: 0px; padding: 6px; overflow: auto; width: 500px; height: 82px; text-align: left;" dir="ltr"><span style="color: #ff8000;">&lt;form enctype=<span style="color: #0000ff;">"multipart/form-data"</span> action=<span style="color: #0000ff;">"insert.php"</span> method=<span style="color: #0000ff;">"post"</span> name=<span style="color: #0000ff;">"changer"</span>&gt;</span>

<span style="color: #ff8000;">&lt;input name=<span style="color: #0000ff;">"MAX_FILE_SIZE"</span> value=<span style="color: #0000ff;">"102400"</span> type=<span style="color: #0000ff;">"hidden"</span>&gt;</span>
<span style="color: #ff8000;">&lt;input name=<span style="color: #0000ff;">"image"</span> accept=<span style="color: #0000ff;">"image/jpeg"</span> type=<span style="color: #0000ff;">"file"</span>&gt;</span>

<span style="color: #ff8000;">&lt;input value=<span style="color: #0000ff;">"Submit"</span> type=<span style="color: #0000ff;">"submit"</span>&gt;</span></pre>
</div>
<p>Copy this code into a file and save it as <strong>add.html</strong></p>
<p>The code above allows the uses to browse for a file and select any image/jpeg file type.   Once selected and the submit button is pressed the image contents are forwarded to PHP script, <strong>insert.php</strong></p>
<p><span style="font-size: medium;"><strong>Inserting the Image into MySQL</strong></span></p>
<p>Create a new file named insert.php.  If you change the name of this file you will also need to change the action=&#8221;" part in your HTML file above.  Open the file in your favorite text editor or PHP IDE.</p>
<p>Write or copy/paste this code into your file:</p>
<div style="margin: 5px 20px 20px;">
<div class="smallfont" style="margin-bottom: 2px;">PHP Code:</div>
<div class="alt2" style="border: 1px inset; margin: 0px; padding: 6px; overflow: auto; width: 500px; height: 274px; text-align: left;" dir="ltr"><code style="white-space: nowrap;"><br />
<!-- php buffer start --></code><code><span style="color: #000000;"><br />
<span style="color: #0000bb;"> </span><span style="color: #ff8000;">// Create MySQL login values and<br />
// set them to your login information.<br />
</span><span style="color: #0000bb;">$username </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"YourUserName"</span><span style="color: #007700;">;<br />
</span><span style="color: #0000bb;">$password </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"YourPassword"</span><span style="color: #007700;">;<br />
</span><span style="color: #0000bb;">$host </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"localhost"</span><span style="color: #007700;">;<br />
</span><span style="color: #0000bb;">$database </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"binary"</span><span style="color: #007700;">;</p>
<p></span><span style="color: #ff8000;">// Make the connect to MySQL or die<br />
// and display an error.<br />
</span><span style="color: #0000bb;">$link </span><span style="color: #007700;">= </span><span style="color: #0000bb;">mysql_connect</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$host</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$username</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$password</span><span style="color: #007700;">);<br />
if (!</span><span style="color: #0000bb;">$link</span><span style="color: #007700;">) {<br />
die(</span><span style="color: #dd0000;">'Could not connect: ' </span><span style="color: #007700;">. </span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());<br />
}</p>
<p></span><span style="color: #ff8000;">// Select your database<br />
</span><span style="color: #0000bb;">mysql_select_db </span><span style="color: #007700;">(</span><span style="color: #0000bb;">$database</span><span style="color: #007700;">);</p>
<p></span><span style="color: #0000bb;"> </span><br />
</span><br />
</code><!-- php buffer end --></div>
</div>
<p>The above code sets your connection variables, creates a connection to MySQL and selects your database (binary if you followed my instructions above).  Next we need to read the form data and insert it into the database.</p>
<div style="margin: 5px 20px 20px;">
<div class="smallfont" style="margin-bottom: 2px;">PHP Code:</div>
<div class="alt2" style="border: 1px inset; margin: 0px; padding: 6px; overflow: auto; width: 500px; height: 498px; text-align: left;" dir="ltr">
<p><code style="white-space: nowrap;"><br />
<!-- php buffer start --></code><code><span style="color: #000000;"><br />
<span style="color: #0000bb;"> </span><span style="color: #ff8000;">// Make sure the user actually<br />
// selected and uploaded a file<br />
</span><span style="color: #007700;">if (isset(</span><span style="color: #0000bb;">$_FILES</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'image'</span><span style="color: #007700;">]) &amp;&amp; </span><span style="color: #0000bb;">$_FILES</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'image'</span><span style="color: #007700;">][</span><span style="color: #dd0000;">'size'</span><span style="color: #007700;">] &gt; </span><span style="color: #0000bb;">0</span><span style="color: #007700;">) {</span></span></code></p>
<p><span style="color: #ff8000;">// Temporary file name stored on the server<br />
</span><span style="color: #0000bb;">$tmpName </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$_FILES</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'image'</span><span style="color: #007700;">][</span><span style="color: #dd0000;">'tmp_name'</span><span style="color: #007700;">];</span></p>
<p><span style="color: #ff8000;">// Read the file<br />
</span><span style="color: #0000bb;">$fp </span><span style="color: #007700;">= </span><span style="color: #0000bb;">fopen</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$tmpName</span><span style="color: #007700;">, </span><span style="color: #dd0000;">&#8216;r&#8217;</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">$data </span><span style="color: #007700;">= </span><span style="color: #0000bb;">fread</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$fp</span><span style="color: #007700;">, </span><span style="color: #0000bb;">filesize</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$tmpName</span><span style="color: #007700;">));<br />
</span><span style="color: #0000bb;">$data </span><span style="color: #007700;">= </span><span style="color: #0000bb;">addslashes</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$data</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">fclose</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$fp</span><span style="color: #007700;">);</span></p>
<p><span style="color: #ff8000;">// Create the query and insert<br />
// into our database.<br />
</span><span style="color: #0000bb;">$query </span><span style="color: #007700;">= </span><span style="color: #dd0000;">&#8220;INSERT INTO tbl_images &#8221;</span><span style="color: #007700;">;<br />
</span><span style="color: #0000bb;">$query </span><span style="color: #007700;">.= </span><span style="color: #dd0000;">&#8220;(image) VALUES (&#8216;$data&#8217;)&#8221;</span><span style="color: #007700;">;<br />
</span><span style="color: #0000bb;">$results </span><span style="color: #007700;">= </span><span style="color: #0000bb;">mysql_query</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$query</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$link</span><span style="color: #007700;">);</span></p>
<p><span style="color: #ff8000;">// Print results<br />
</span><span style="color: #007700;">print </span><span style="color: #dd0000;">&#8220;Thank you, your file has been uploaded.&#8221;</span><span style="color: #007700;">;</span></p>
<p>}<br />
else {<br />
print <span style="color: #dd0000;">&#8220;No image selected/uploaded&#8221;</span><span style="color: #007700;">;<br />
}</span></p>
<p><span style="color: #ff8000;">// Close our MySQL Link<br />
</span><span style="color: #0000bb;">mysql_close</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$link</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">?&gt;</span></p>
<p><!-- php buffer end --></div>
</div>
<p>The PHP code above takes the user selected image, reads it with fopen and stores the data in a variable named $data.  The binary data is then inserted into our database for retrieval at a later time.</p>
<p>Save the file above and access add.html via your browser.  Select an image file and upload it.</p>
<p>I will post the next stage of this tutorial: &#8220;<a href="http://otakbali.com/display Images-from-mySQL-binary-data"><strong>How to read binary data from database</strong></a>&#8220;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.otakbali.com/storing-imagesbinary-files-to-mysql-in-php.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
