<?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>Rony&#039;s &#187; MySQL</title>
	<atom:link href="http://rony.creash.com.bd/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://rony.creash.com.bd</link>
	<description>Blogor Blogor</description>
	<lastBuildDate>Fri, 26 Mar 2010 21:34:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>File upload/download on MySQL database</title>
		<link>http://rony.creash.com.bd/file-uploaddownload-on-mysql-database/</link>
		<comments>http://rony.creash.com.bd/file-uploaddownload-on-mysql-database/#comments</comments>
		<pubDate>Fri, 28 Nov 2008 14:35:39 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Download]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Upload]]></category>

		<guid isPermaLink="false">http://rony.creash.com.bd/?p=48</guid>
		<description><![CDATA[Well&#8230; It is already a well discussed topic. But when I was first trying to find out how to do this, I really got stuck. Actully it took me a lot of time to find out the key code segment for the purpose.
So, here is what I&#8217;ve learned:
The table:
Suppose the structure of the MySQL table [...]]]></description>
			<content:encoded><![CDATA[<p>Well&#8230; It is already a well discussed topic. But when I was first trying to find out how to do this, I really got stuck. Actully it took me a lot of time to find out the key code segment for the purpose.</p>
<p>So, here is what I&#8217;ve learned:</p>
<p><strong>The table:</strong></p>
<p>Suppose the structure of the MySQL table (say &#8216;<em>file_table</em>&#8216;) is like this:</p>
<p>It has three fileds:</p>
<ul>
<li><code>id : the primary key, integer type with auto increment feature.</code></li>
<li><code>content: the content of file, blob type.</code></li>
<li><code>type: varchar type, the type of the file (e.g. doc, pdf etc.).<br />
</code></li>
</ul>
<p><strong>Upload:</strong></p>
<p>I think you can create a HTML form with a input with file type. Its pretty simple. Just use this code to make a input field be able to upload file:</p>
<p><code>&lt;input id="file_upload" name="file_upload" type="file" /&gt;</code></p>
<p>Now the backend coding. I&#8217;m using PHP for this.<span id="more-48"></span></p>
<p>You can get the temp file name (the name server assigned to your uploaded file) using this:</p>
<p><code>$tmpName = $_FILES['file_upload']['tmp_name'];</code></p>
<p>Now following code segment reads the content of the file to a variable:</p>
<p><code>$fp = fopen($tmpName, 'r');<br />
$file_size =  filesize($tmpName);<br />
$content = fread($fp, $file_size);<br />
$content = addslashes($content);<br />
fclose($fp);</code><br />
<code><br />
if(!get_magic_quotes_gpc())<br />
{<br />
$fileName = addslashes($fileName);<br />
}</code></p>
<p>To get the file type, we use this segment of code:</p>
<p><code>$type = get_file_extension($tmpName);</code></p>
<p><code>function get_file_extension($str)<br />
{<br />
$i = strrpos($str,".");<br />
if (!$i) { return ""; }<br />
$l = strlen($str) - $i;<br />
$ext = substr($str,$i+1,$l);<br />
return $ext;<br />
}</code></p>
<p>Now use this query to insert the file to database:</p>
<p><code>$query = 'INSERT INTO file_table VALUES (" ' . $content . ' ", " ' . $type . ' ")';<br />
$result = @mysql_query($query) or die(mysql_error());</code></p>
<p>That&#8217;s it. We&#8217;ve uploaded our file to the database.</p>
<p><strong>Download:</strong></p>
<p>We&#8217;ve uploaded the file. Now we need to download it sometime. Thats not that hard actually.</p>
<p>We&#8217;ll follow these stpes:</p>
<ul>
<li>Get the id of desired file</li>
<li>Fetch the file from database on the basis of the id</li>
<li>Make the file ready for download</li>
</ul>
<p>Say, we get the id from URL. Then following code will make the file available for download:</p>
<p><code>$id    = $_GET['id'];<br />
$query = "SELECT content, type FROM file_table WHERE id = '$id' ";</code></p>
<p><code>$result = mysql_query($query) or die(mysql_error());<br />
list($file, $ftype) = mysql_fetch_array($result);</code></p>
<p><code>header("Content-length: $filesize");<br />
header("Content-type: $ftype");<br />
header("Content-Disposition: attachment; filename = FILENAME.$ftype");<br />
echo $file;</code></p>
<p>Here we set the header with the file information. Make sure, nothing else is printed on the page before the <em>header()</em> call. Otherwise it will show an error and terminate. You can place your desired file name at the place of &#8216;<em>FILENAME</em>&#8216;. Another thing is, connection with the database server is to be made before any database operation. I think you know the function:</p>
<p><code>mysql_connect();</code></p>
<p>If not can check this post: <a href="http://rony.creash.com.bd/?p=42#top3" target="_blank">Basic PHP</a></p>
<!-- Social Bookmarking Reloaded BEGIN --><div class="social_bookmark"><em>Bookmark:</em><br /><a class="social_img" onclick="pageTracker._trackPageview('/outgoing/del.icio.us/post?url=http_//rony.creash.com.bd/file-uploaddownload-on-mysql-database/_amp_title=File+upload_2Fdownload+on+MySQL+database&amp;referer=');window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://rony.creash.com.bd/file-uploaddownload-on-mysql-database/&amp;title=File+upload%2Fdownload+on+MySQL+database" title="Add 'File upload/download on MySQL database' to Del.icio.us"><img src="http://rony.creash.com.bd/wp-content/plugins/social-bookmarking-reloaded/delicious.png" title="Add 'File upload/download on MySQL database' to Del.icio.us" alt="Add 'File upload/download on MySQL database' to Del.icio.us" /></a><a class="social_img" onclick="pageTracker._trackPageview('/outgoing/digg.com/submit?phase=2_amp_url=http_//rony.creash.com.bd/file-uploaddownload-on-mysql-database/_amp_title=File+upload_2Fdownload+on+MySQL+database&amp;referer=');window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://rony.creash.com.bd/file-uploaddownload-on-mysql-database/&amp;title=File+upload%2Fdownload+on+MySQL+database" title="Add 'File upload/download on MySQL database' to digg"><img src="http://rony.creash.com.bd/wp-content/plugins/social-bookmarking-reloaded/digg.png" title="Add 'File upload/download on MySQL database' to digg" alt="Add 'File upload/download on MySQL database' to digg" /></a><a class="social_img" onclick="pageTracker._trackPageview('/outgoing/www.technorati.com/faves?add=http_//rony.creash.com.bd/file-uploaddownload-on-mysql-database/&amp;referer=');window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://rony.creash.com.bd/file-uploaddownload-on-mysql-database/" title="Add 'File upload/download on MySQL database' to Technorati"><img src="http://rony.creash.com.bd/wp-content/plugins/social-bookmarking-reloaded/technorati.png" title="Add 'File upload/download on MySQL database' to Technorati" alt="Add 'File upload/download on MySQL database' to Technorati" /></a><a class="social_img" onclick="pageTracker._trackPageview('/outgoing/www.stumbleupon.com/submit?url=http_//rony.creash.com.bd/file-uploaddownload-on-mysql-database/_amp_title=File+upload_2Fdownload+on+MySQL+database&amp;referer=');window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://rony.creash.com.bd/file-uploaddownload-on-mysql-database/&amp;title=File+upload%2Fdownload+on+MySQL+database" title="Add 'File upload/download on MySQL database' to Stumble Upon"><img src="http://rony.creash.com.bd/wp-content/plugins/social-bookmarking-reloaded/stumbleupon.png" title="Add 'File upload/download on MySQL database' to Stumble Upon" alt="Add 'File upload/download on MySQL database' to Stumble Upon" /></a><a class="social_img" onclick="pageTracker._trackPageview('/outgoing/www.google.com/bookmarks/mark?op=edit_amp_output=popup_amp_bkmk=http_//rony.creash.com.bd/file-uploaddownload-on-mysql-database/_amp_title=File+upload_2Fdownload+on+MySQL+database&amp;referer=');window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://rony.creash.com.bd/file-uploaddownload-on-mysql-database/&amp;title=File+upload%2Fdownload+on+MySQL+database" title="Add 'File upload/download on MySQL database' to Google Bookmarks"><img src="http://rony.creash.com.bd/wp-content/plugins/social-bookmarking-reloaded/google.png" title="Add 'File upload/download on MySQL database' to Google Bookmarks" alt="Add 'File upload/download on MySQL database' to Google Bookmarks" /></a><a class="social_img" onclick="pageTracker._trackPageview('/outgoing/slashdot.org/bookmark.pl?title=File+upload_2Fdownload+on+MySQL+database_amp_url=http_//rony.creash.com.bd/file-uploaddownload-on-mysql-database/&amp;referer=');window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?title=File+upload%2Fdownload+on+MySQL+database&amp;url=http://rony.creash.com.bd/file-uploaddownload-on-mysql-database/" title="Add 'File upload/download on MySQL database' to SlashDot"><img src="http://rony.creash.com.bd/wp-content/plugins/social-bookmarking-reloaded/slashdot.png" title="Add 'File upload/download on MySQL database' to SlashDot" alt="Add 'File upload/download on MySQL database' to SlashDot" /></a><a class="social_img" onclick="pageTracker._trackPageview('/outgoing/www.facebook.com/share.php?u=http_//rony.creash.com.bd/file-uploaddownload-on-mysql-database/_amp_t=File+upload_2Fdownload+on+MySQL+database&amp;referer=');window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/share.php?u=http://rony.creash.com.bd/file-uploaddownload-on-mysql-database/&amp;t=File+upload%2Fdownload+on+MySQL+database" title="Add 'File upload/download on MySQL database' to FaceBook"><img src="http://rony.creash.com.bd/wp-content/plugins/social-bookmarking-reloaded/facebook.png" title="Add 'File upload/download on MySQL database' to FaceBook" alt="Add 'File upload/download on MySQL database' to FaceBook" /></a><a class="social_img" onclick="pageTracker._trackPageview('/outgoing/www.myspace.com/Modules/PostTo/Pages/?t=File+upload_2Fdownload+on+MySQL+database_amp_c=http_//rony.creash.com.bd/file-uploaddownload-on-mysql-database/&amp;referer=');window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.myspace.com/Modules/PostTo/Pages/?t=File+upload%2Fdownload+on+MySQL+database&amp;c=http://rony.creash.com.bd/file-uploaddownload-on-mysql-database/" title="Add 'File upload/download on MySQL database' to MySpace"><img src="http://rony.creash.com.bd/wp-content/plugins/social-bookmarking-reloaded/myspace.png" title="Add 'File upload/download on MySQL database' to MySpace" alt="Add 'File upload/download on MySQL database' to MySpace" /></a><a class="social_img" onclick="pageTracker._trackPageview('/outgoing/twitter.com/home?status=http_//rony.creash.com.bd/file-uploaddownload-on-mysql-database/&amp;referer=');window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home?status=http://rony.creash.com.bd/file-uploaddownload-on-mysql-database/" title="Add 'File upload/download on MySQL database' to Twitter"><img src="http://rony.creash.com.bd/wp-content/plugins/social-bookmarking-reloaded/twitter.png" title="Add 'File upload/download on MySQL database' to Twitter" alt="Add 'File upload/download on MySQL database' to Twitter" /></a><a class="social_img" onclick="pageTracker._trackPageview('/outgoing/www.google.com/reader/link?url=http_//rony.creash.com.bd/file-uploaddownload-on-mysql-database/_amp_title=File+upload_2Fdownload+on+MySQL+database_amp_srcURL=http_//rony.creash.com.bd/file-uploaddownload-on-mysql-database/&amp;referer=');window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/reader/link?url=http://rony.creash.com.bd/file-uploaddownload-on-mysql-database/&amp;title=File+upload%2Fdownload+on+MySQL+database&amp;srcURL=http://rony.creash.com.bd/file-uploaddownload-on-mysql-database/" title="Add 'File upload/download on MySQL database' to Google Buzz"><img src="http://rony.creash.com.bd/wp-content/plugins/social-bookmarking-reloaded/googlebuzz.png" title="Add 'File upload/download on MySQL database' to Google Buzz" alt="Add 'File upload/download on MySQL database' to Google Buzz" /></a></div>
<!-- Social Bookmarking Reloaded END -->]]></content:encoded>
			<wfw:commentRss>http://rony.creash.com.bd/file-uploaddownload-on-mysql-database/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
