<?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>Albert Fama &#187; validation trick</title>
	<atom:link href="http://albertfama.com/tag/validation-trick/feed/" rel="self" type="application/rss+xml" />
	<link>http://albertfama.com</link>
	<description>Freelance Web Programmer - specializing in PHP &#38; MySQL</description>
	<lastBuildDate>Fri, 20 Nov 2009 16:06:31 +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>Trick: Image Uploads</title>
		<link>http://albertfama.com/php/trick-image-uploads/</link>
		<comments>http://albertfama.com/php/trick-image-uploads/#comments</comments>
		<pubDate>Thu, 13 Dec 2007 22:40:18 +0000</pubDate>
		<dc:creator>Albert Fama</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[file upload]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[validation]]></category>
		<category><![CDATA[validation trick]]></category>

		<guid isPermaLink="false">http://obnexus.net/?p=43</guid>
		<description><![CDATA[We have all written scripts to upload files to a server, and for one reason or another I find that most of the time the script is intended for image uploads. Whenever you are uploading user files to the server it is very important that those files are validated to ensure the uploaded file is [...]]]></description>
			<content:encoded><![CDATA[<p>We have all written scripts to upload files to a server, and for one reason or another I find that most of the time the script is intended for image uploads. Whenever you are uploading user files to the server it is very important that those files are validated to ensure the uploaded file is actually what is expected. Since just checking the HTTP headers and the file&#8217;s extensions is insufficient we need to find another way to determine if the file is actually an image file.</p>
<p>While writing some validation code I came across this &#8220;trick&#8221; which can be used to determine if a file is an image file: Send the uploaded file through <a href="http://www.php.net/manual/en/function.getimagesize.php"  title="PHP Manual: getimagesize">getimagesize()</a> and check the return values. </p>
<p>As the PHP Manual states:</p>
<blockquote><p><strong>PHP Manual</strong></p>
<p>The getimagesize() function will determine the size of any given image file and return the dimensions along with the file type and a height/width text string to be used inside a normal HTML IMG tag and the correspondant HTTP content type.</p></blockquote>
<p>If PHP cannot access the file or the file is not an image, the function will generate an E_WARNING error, and return boolean FALSE. The E_WARNING error can be suppressed by using the error suppression operator &#8216;@&#8217;. As long as you know that the file is accessible to PHP (which it should be if PHP uploaded the file) and the function does not return FALSE, then you have a valid image file.</p>
<p>I first leaned about this method from the book <a rel="nofollow" href="http://www.amazon.com/php-architects-Guide-PHP-Security/dp/0973862106/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1197585311&#038;sr=1-1"  title="Amazon: php|architect's Guide to PHP Security">php|architect&#8217;s Guide to PHP Security</a> by <a href="http://ilia.ws/"  title="Personal Website: Ilia Alshanetsky">Ilia Alshanetsky</a>.</p>

                            <div id="aspdf">
                                <a href="http://albertfama.com/wp-content/plugins/as-pdf/generate.php?post=43">
                                    <span>&nbsp;</span>
                                </a>
                            </div>
                        ]]></content:encoded>
			<wfw:commentRss>http://albertfama.com/php/trick-image-uploads/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

