<?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>James Conzett &#187; php code</title>
	<atom:link href="http://jamesconzett.com/index.php/tag/php-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://jamesconzett.com</link>
	<description>Luck is when preperation meets opportunity</description>
	<lastBuildDate>Thu, 29 Mar 2012 14:13:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Get a files extention with PHP</title>
		<link>http://jamesconzett.com/index.php/get-a-files-extention-with-php/</link>
		<comments>http://jamesconzett.com/index.php/get-a-files-extention-with-php/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 11:33:53 +0000</pubDate>
		<dc:creator>Jim</dc:creator>
				<category><![CDATA[PHP Code Snippets]]></category>
		<category><![CDATA[filename]]></category>
		<category><![CDATA[get file extention]]></category>
		<category><![CDATA[get file name only]]></category>
		<category><![CDATA[image type]]></category>
		<category><![CDATA[pathinfo]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php code]]></category>
		<category><![CDATA[return extention]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[strip a files name]]></category>
		<category><![CDATA[strip file name]]></category>

		<guid isPermaLink="false">http://www.jamesconzett.com/?p=108</guid>
		<description><![CDATA[There are several ways determine a file extension using PHP. First is using the combination of strrpos() and substr() function like this :
$ext = substr($fileName, strrpos($fileName, &#8216;.&#8217;) + 1);
For example, if $fileName is my-new-house.jpg then:
strrpos($fileName, &#8216;.&#8217;)
The above will return the last location a dot character in $fileName which is 15.
So&#8230;&#8230;
substr($fileName, strrpos($fileName, &#8216;.&#8217;) + 1) 
equals [...]]]></description>
			<content:encoded><![CDATA[<p>There are several ways determine a file extension using PHP. First is using the combination of strrpos() and substr() function like this :</p>
<p><strong>$ext = substr($fileName, strrpos($fileName, &#8216;.&#8217;) + 1);</strong></p>
<p>For example, if $fileName is my-new-house.jpg then:<br />
<strong>strrpos($fileName, &#8216;.&#8217;)</strong><br />
The above will return the last location a dot character in $fileName which is 15.</p>
<p>So&#8230;&#8230;<br />
<strong>substr($fileName, strrpos($fileName, &#8216;.&#8217;) + 1) </strong><br />
equals substr($fileName, 16) which returns &#8216;jpg&#8217;</p>
<p>The second is using strrchr() and substr() :</p>
<p><strong>$ext = substr(strrchr($fileName, &#8216;.&#8217;), 1);</strong><br />
strrchr($fileName) returns &#8216;.jpg&#8217; so:<br />
<strong>substr(strrchr($fileName, &#8216;.&#8217;), 1)</strong> equals:<br />
substr(&#8216;.jpg&#8217;, 1) which returns &#8216;jpg&#8217;.</p>
<p>Savvy?</p>
<p>Now, in PHP 5.2.0 <strong>PATHINFO_FILENAME</strong> was added! Hooray!</p>
<p>$FileName = pathinfo($_FILES['File']['name'], PATHINFO_FILENAME);</p>
<p>So&#8230;<br />
<strong>$file = &#8220;somefile.jpg&#8221;;<br />
$extension = pathinfo($file, PATHINFO_EXTENSION); </strong><br />
Returns the file extention!</p>
<p>So:<br />
<strong>$path_parts = pathinfo(&#8216;/www/htdocs/index.html&#8217;);<br />
echo $path_parts['dirname'], &#8220;n&#8221;;<br />
echo $path_parts['basename'], &#8220;n&#8221;;<br />
echo $path_parts['extension'], &#8220;n&#8221;;<br />
echo $path_parts['filename'], &#8220;n&#8221;;</strong></p>
<p>Will return:<br />
/www/htdocs<br />
index.html<br />
html<br />
index</p>
<p>Blow is a simple function that gets the extension of a file. Simply using PATHINFO_EXTENSION will yield incorrect results if the path contains a query string with dots in the parameter names (for eg. &#038;x.1=2&#038;y.1=5), so this function eliminates the query string first and subsequently runs PATHINFO_EXTENSION on the clean path/url.</p>
<p><strong>function extension($path) {<br />
$qpos = strpos($path, &#8220;?&#8221;);<br />
if ($qpos!==false) $path = substr($path, 0, $qpos);<br />
$extension = pathinfo($path, PATHINFO_EXTENSION);<br />
return $extension;<br />
}<br />
</strong></p>
<p>Now, what if the filename has an extra &#8220;.&#8221; in it??? Like: &#8220;some.file.jpg&#8221;</p>
<p>The below will reverse the filename and create a substring from it that starts after the first &#8220;.&#8221;.  eg: &#8220;gpj.elif.emos&#8221;<br />
Then we pull off the data and reverse it back!</p>
<p><strong>if(!isset($path_parts['filename'])){<br />
    $reversed_filename = strrev( $path_parts['basename'] );<br />
    $path_parts['filename'] = strrev( substr( $reversed_filename, strpos( $reversed_filename, &#8216;.&#8217; ) + 1 ) );<br />
}</strong></p>
<p>Nuff said?</p>
]]></content:encoded>
			<wfw:commentRss>http://jamesconzett.com/index.php/get-a-files-extention-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

