<?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>Design Shack &#187; PHP</title>
	<atom:link href="http://designshack.net/category/articles/php-articles/feed" rel="self" type="application/rss+xml" />
	<link>http://designshack.net</link>
	<description>Inspiration, CSS Gallery &#38; Community News</description>
	<lastBuildDate>Thu, 09 Feb 2012 06:00:00 +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>Adding an About the Author Box to Your WordPress Posts</title>
		<link>http://designshack.net/articles/php-articles/adding-an-about-the-author-box-to-your-wordpress-posts/</link>
		<comments>http://designshack.net/articles/php-articles/adding-an-about-the-author-box-to-your-wordpress-posts/#comments</comments>
		<pubDate>Fri, 29 Jul 2011 17:26:29 +0000</pubDate>
		<dc:creator>Jake Rocheleau</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[author]]></category>
		<category><![CDATA[modules]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[writing]]></category>

		<guid isPermaLink="false">http://designshack.co.uk/?p=22934</guid>
		<description><![CDATA[Digital magazines have become a popular commodity for today&#8217;s web users. With WordPress powering millions of blogs on the web today, it&#8217;s fair to assume they run a stable piece of software. Some of the best features are all internal as the WP team publishes live, updated documentation with each release. Not only is the [...]]]></description>
			<content:encoded><![CDATA[<p>Digital magazines have become a popular commodity for today&#8217;s web users. With WordPress powering millions of blogs on the web today, it&#8217;s fair to assume they run a stable piece of software.  Some of the best features are all internal as the WP team publishes live, updated documentation with each release.</p>
<p>Not only is the software very powerful but the underlying classes and functions give developers an all-access pass. It is extremely simple to develop apps and modules within WordPress. Today we&#8217;ll be looking into author meta data functions for building an &#8220;About the Author&#8221; box.</p>
<p><span id="more-22934"></span><br />
<em>Like the article? Be sure to subscribe to our <a href="feed://feeds.feedburner.com/designshack">RSS feed</a> and follow us on <a href="http://twitter.com/designshack">Twitter</a> to stay up on recent content.</em></p>
<div class="tutorialimage"><a href="http://www.onextrapixel.com/"><img class="alignNone size-full" src="http://designshack.net/wp-content/uploads/oxp-about-the-author-post-details.jpg" alt="Onextra Pixel Author box" width="510" /></a></div>
<p>Towards the end of single post pages you may frequently see a box explaining a bit about the writer. This will generally include their Gravatar or some sort of photo, a description about them, and possibly links to their profiles or personal website.</p>
<p>If things seems difficult check out some of WordPress&#8217; <a href="http://codex.wordpress.org/Template_Tags">documentation on template tags</a>. These include much of what can be covered in this project and a whole lot more. The WP community also runs a very pleasant forum for in-depth system questions.</p>
<h2>The WordPress Architecture</h2>
<p>There is something referenced throughout WordPress code titled <a href="http://codex.wordpress.org/The_Loop">The Loop</a>. This is a section where meta functions can be called based on a single article to pull down specific information from the database. As per our example we can call functions to pull out author data from any single post within The Loop.</p>
<p>Most notably this should be done within your template folder. For the lazy, it may be easier to edit individual files by adding code you&#8217;ll need. For example, the WP theme <code>single.php</code> file generally contains all code to parse on an individual posts&#8217; page. You may find it easier to pull data while inside The Loop and add HTML code here.</p>
<p>Alternatively, a safer method would be to add a new file to your theme directory and include it whenever you&#8217;d want your author box to appear. This allows for greater control regarding variable terms and template files. This also saves you time and energy so as to not go back and re-invent the wheel.</p>
<h2>WP Template Code</h2>
<p>There aren&#8217;t many functions we&#8217;ll be using, though many could be new to WP amateurs. Below are just a few examples which you should familiarize yourself with. Much of what we&#8217;ll be covering works within the WordPress library with just a few key ideas – nothing that can&#8217;t be learned with a few hours of study.</p>
<div style="overflow: auto;background-color: #eeeeee">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> the_author<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</div>
<p>The author function pulls data from within the loop to display the author&#8217;s name. This can be changed in the WordPress backend on anybody&#8217;s profile settings page. You are able to set this value to First/Last name, username, or even a custom set.</p>
<div class="tutorialimage"><a href="http://freelanceswitch.com/"><img class="alignNone size-full" src="http://designshack.net/wp-content/uploads/freelanceswitch-blog-footer-author.jpg" alt="Freelance Switch Blog - Author Box" width="510" /></a></div>
<p>To make this process simpler, WordPress has released an entire function library for <code>the_author_meta()</code>. This replacement takes in arguments which can pull any author data from the database on request. You can check out <a href="http://codex.wordpress.org/Function_Reference/the_author_meta">the page documentation</a> if you&#8217;d like to research more into the subject.</p>
<p>What&#8217;s fascinating about this function is how much information can actually be obtained. By default the function accepts two arguments: the type of data you need and the user ID to pull. The ID is an optional parameter which is only required in cases outside of The Loop. Possible values to pull include user_login, user_email, user_url, nickname, description (bio), jabber/aim/yim, and so much more!</p>
<div class="tutorialimage"><a href="http://webdesign.tutsplus.com/"><img class="alignNone size-full" src="http://designshack.net/wp-content/uploads/tutsplus-design-author.jpg" alt="Author Box after Web Design Tuts+ Network" width="510" /></a></div>
<p>What many don&#8217;t consider here is just how much potential information can be shared. WordPress allows for administrators to edit author profile pages to add in custom avatars, bios, and even home page links. These can then be pulled from the database and used as marketing information for each author.</p>
<p>With just these functions alone we are able to display all of the set data we will need. The only thing left to do is add our sample HTML and CSS code and publish the file!</p>
<h2>Cordial Author Box Styles</h2>
<p>CSS is an important language in our project scope, being a web application. We need to define specific sets of styles and display cases to incorporate for all major browsers, monitor resolutions, and other hidden variables. As web developers it is our job to study and work over these climates.</p>
<div style="overflow: auto;background-color: #eeeeee">

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.aboutauthor</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#f4f4f4</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#999</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">margin-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.aboutauthor</span> img <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#aboutauthorimg</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">5px</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.aboutauthor</span> <span style="color: #6666ff;">.span-desc</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">500px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.aboutauthor</span> <span style="color: #6666ff;">.span-2</span> <span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span> <span style="color: #933;">10px</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

</div>
<p>The simple class <em>.aboutauthor</em> should be applied to your covering box. We have added a light background color with a solid border and extra padding. Our img tag is also displayed floating left with extra padding. This will keep our author&#8217;s avatar in line with box content.</p>
<div class="tutorialimage"><a href="http://www.noupe.com/"><img class="alignNone size-full" src="http://designshack.net/wp-content/uploads/noupe-author-box-design.jpg" alt="Noupe Post Author Box" width="510" /></a></div>
<p>Further, we have two classes which demonstrate how we need our inner data to perform. <em>.span-2</em> is a class held inside where our main content will be found. Inside here we have removed all extra margins/padding to keep spacing neutral, along with rearranging the box model for left alignment. This code would do best inside your template&#8217;s style.css file, though it could be added inline with your block element.</p>
<p>The HTML code is very short and similar as seen below. Here we have added a few extra functions which can be explained very quickly.</p>
<div style="overflow: auto;background-color: #eeeeee">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;div class=&quot;aboutauthor&quot;&gt;
&lt;div id=&quot;aboutauthorimg&quot; class=&quot;span-2&quot;&gt;&lt;/div&gt;
&nbsp;
&lt;div class=&quot;span-desc&quot;&gt;About Author: &lt;strong&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_author<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #339933;">&gt;&lt;/</span>strong<span style="color: #339933;">&gt;</span>  <span style="color: #009900;">&#40;</span><span style="color: #339933;">;</span> Articles<span style="color: #009900;">&#41;</span>
<span style="color: #339933;">&lt;</span>strong<span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php the_author_meta<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'display_name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #339933;">&gt;&lt;/</span>strong<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>

</div>
<p>We are calling a function <code>get_avatar()</code> which takes a set of parameters. The two we have passed above are the current author&#8217;s e-mail address which is the unique identifier for a database query. This could be equivalent to a user&#8217;s ID, too. We also pass in 64 as a string value – this represents the size of the image we want returned squared off in width and height.</p>
<div class="tutorialimage"><a href="http://mac.appstorm.net/"><img class="alignNone size-full" src="http://designshack.net/wp-content/uploads/mac-appstorm-author-info.jpg" alt="Mac AppStorm Author Profile Page" width="510" /></a></div>
<p><code>the_author_posts()</code> is another fairly straightforward function which pulls the total number of articles a writer has published. This is more of an added feature as I don&#8217;t see many blogs running this info. Commonly, though, this is useful from a users&#8217; perspective since it gives more information about the current author and deeper insight into their interaction with the blog itself.</p>
<p>These are just some of the basics techniques for building a simple WordPress author block. From here it&#8217;s very simple to save a new file, let&#8217;s say author-box.php into your themes folder and upload into your root directory. From there a simple PHP include can add all of your code right into any source automatically.</p>
<div class="tutorialimage"><a href="http://www.blog.spoongraphics.co.uk/"><img class="alignNone size-full" src="http://designshack.net/wp-content/uploads/spoongraphics-about-the-author.jpg" alt="Chris Spooner SpoonGraphics Author Box" width="510" /></a></div>
<div style="overflow: auto;background-color: #eeeeee">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span>get_bloginfo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;template_url&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/author-box.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</div>
<p>If this is still a bit fuzzy, check out the example code above. This would be the most direct way to include your file and sustain security in your actions. Here we are calling the <code>get_bloginfo()</code> function to pull our entire template directory structure out first, followed by our single data file. Be sure to rename this file to whatever will suit your theme best and don&#8217;t get confused between function calls!</p>
<h2>Conclusion</h2>
<p>Hopefully this tutorial has given you a starting point for reference in WordPress development. Blogs are all too common in today&#8217;s web, but adding an author box gives your online articles some flare. They also give incentive for authors to post more frequently and build up a small portfolio of their writing. Many of the most popular magazines online now feature detailed information and profile links from their article writers.</p>
<p>If you&#8217;d like to experiment with these features without messing around in PHP or HTML/CSS, there are a few alternatives. The WordPress plugins system is one such scenario which works perfectly. I recommend the <a href="http://wordpress.org/extend/plugins/cool-author-box/">Cool Author Box</a> plugin which will automatically add author info and supports gravatar icons. Do you have any suggestions or tips for building unique author boxes in WordPress? Share your ideas in the discussion below.</p>
]]></content:encoded>
			<wfw:commentRss>http://designshack.net/articles/php-articles/adding-an-about-the-author-box-to-your-wordpress-posts/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Accessing Foursquare API Data via OAuth and PHP</title>
		<link>http://designshack.net/articles/php-articles/accessing-foursquare-api-data-via-oauth-and-php/</link>
		<comments>http://designshack.net/articles/php-articles/accessing-foursquare-api-data-via-oauth-and-php/#comments</comments>
		<pubDate>Tue, 29 Mar 2011 17:00:52 +0000</pubDate>
		<dc:creator>Jake Rocheleau</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://designshack.co.uk/?p=17811</guid>
		<description><![CDATA[In this brief tutorial we&#8217;ll be looking into manipulating website data with PHP. Foursquare offers some great tools for accessing their website&#8217;s API. We&#8217;ll be working with an open connection technology OAuth to authenticate users into our own 3rd party Foursquare apps! Like the article? Be sure to subscribe to our RSS feed and follow [...]]]></description>
			<content:encoded><![CDATA[<p>In this brief tutorial we&#8217;ll be looking into manipulating website data with PHP. Foursquare offers some great tools for accessing their website&#8217;s API.</p>
<p>We&#8217;ll be working with an open connection technology OAuth to authenticate users into our own 3rd party Foursquare apps!<br />
<span id="more-17811"></span><br />
<em>Like the article? Be sure to subscribe to our <a href="feed://feeds.feedburner.com/designshack">RSS feed</a> and follow us on <a href="http://twitter.com/designshack">Twitter</a> to stay up on recent content.</em></p>
<h2>Checking In</h2>
<p>The latest social media craze on the web today is focused around location-based social networking. Companies such as Gowalla and most notably <a href="http://foursquare.com/">Foursquare</a> run off a social grid check-in system. For every visit to your local shopping plaza, pizza shack, park or school you can update your status and earn points towards unlocking badges.</p>
<p>The network pivots traveling the globe into a digital game of sorts. Through Foursquare&#8217;s mobile app you can run your own user account, update check-ins to Twitter or Facebook, upload photos, and so much more! The numbers have been growing rapidly and Foursquare&#8217;s community circles the world.</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/gowalla-art.jpg" alt="social geo tagging Gowalla artwork" width="510"/></div>
<h2>Beginner&#8217;s Resources</h2>
<p>When you first start developing over Application Programming Interfaces there are many hurdles to overcome. Through some fantastic articles Google searches hold the answer to a lot of your basic questions on PHP and API connections.</p>
<p>When discussing Foursquare&#8217;s API I highly recommend their <a href="http://groups.google.com/group/foursquare-api?pli=1">Google groups forum</a>. It comes with a few FAQ pages and great user discussions archived over time. More specifically they offer the <a href="http://groups.google.com/group/foursquare-api/web/api-documentation">web API documentation</a> in an elegant way for novice and professional alike.</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/foursquare-heading-art.jpg" alt="Foursquare alternate web design" width="510"/></div>
<p>To get started you&#8217;ll need a simple PHP web setup. If you&#8217;ve got remote server space you could alternatively host everything elsewhere. However if you&#8217;re impatient and don&#8217;t need Internet access it&#8217;s much simpler to install a local server environment.</p>
<p>For Windows and Mac the <a href="http://www.wampserver.com/en/">WAMP</a> and <a href="http://www.mamp.info/en/index.html">MAMP</a> packages work tremendously well. They install with PHP and MySQL database capabilities running under the Apache web server software.</p>
<h2>External Libraries and OAuth</h2>
<p>We only need a few external files to work with Foursquare efficiently. When building an OAuth application there are many simple steps required to get started. This is an open platform built with simplistic standards in mind.</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/coding-monitors.jpg" alt="programming PHP websites desktop" width="510"/></div>
<p>Below is a general overview for an OAuth connection:</p>
<ul>
<li>Obtain request <strong>key</strong> and <strong>secret</strong> from Foursquare API</li>
<li>Add login/authentication link somewhere on website</li>
<li>User clicks link. Directed to Foursquare and approves/rejects access to their account from 3rd party application</li>
<li>Store now secured <strong>key</strong> and <strong>secret</strong> to call requests from Foursquare database</li>
</ul>
<p>You will need a Foursquare account to obtain your key and secret in the first place. It&#8217;s a free signup, and if you wish to work on the API I&#8217;m sure you&#8217;ve already had an interest in the application! Once logged in check out their <a href="https://foursquare.com/oauth/register">OAuth registration form</a> to create your new app.</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/github-sticker-illustration.jpg" alt="Github social coding" width="510"/></div>
<p>You&#8217;ll need to give three (3) bits of data before obtaining your unique key and secret string pair. The app name, website URL, and return URL. The first two are fairly self explanatory while the <strong>return URL</strong> is simply the page Foursquare will re-direct users back towards after authentication is complete.</p>
<h2>Picking Libraries</h2>
<p>The final step after account creation is figuring out which external libraries to use. The simpler and easier we can access bits of data, the quicker our application will run. Github is a fantastic community where developers share their bits of code with the world.</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/foursquare-mayor-checkin.jpg" alt="Foursquare Major Checkin mobile" width="510"/></div>
<p>Specifically user <a href="https://github.com/jmathai/foursquare-async">jmathai&#8217;s Foursquare async</a> library is perfect for what we&#8217;re doing. You can download the package directly from that page. Inside you&#8217;ll find a few files, namely the core libraries we&#8217;ll need to be working with. Listed below:</p>
<ul>
<li>index.php</li>
<li>callback.php</li>
<li>EpiCurl.php</li>
<li>EpiFoursquare.php</li>
<li>EpiOAuth.php</li>
</ul>
<p>Inside our <strong>index.php</strong> file there are a few fields requiring some input. Towards the top we&#8217;ve got two (2) variables named <strong>$consumer_key</strong> and <strong>$consumer_secret</strong>. Inside you&#8217;ll want to set the string value to whatever Foursquare has set up in your OAuth application settings.</p>
<p>The second file we need to edit is <strong>callback.php</strong>. This will handle a generic callback from Foursquare and allows us to pull information about the authorization request (acceptation/rejection). Towards the top of this file you&#8217;ll see the same variables requesting the same set of values &#8211; seemingly redundant, but useful to keep scripts independent.</p>
<h2>Pulling User Data</h2>
<p>Try loading your <strong>index.php</strong> file inside any browser from your local server. You should see a small page with a login link &#8211; clicking this will redirect to Foursquare where you can choose to allow or deny the connection to your application. Upon acceptance you&#8217;ll be re-directed towards your callback URL and can pull user data at will!</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/jake-rocheleau-foursquare-profile.jpg" alt="Jake Rocheleau 4sq user profile data" width="510"/></div>
<p>As a brief example let&#8217;s examine checkin history. Through these few lines of code we can easily pull the past few checkins from any authorized account and display them onscreen (or possibly store them in a database for easy access!)</p>
<div class="code">
<pre style="font-family:monospace">// call 4sq data
$fsqUsrHistory = $foursquareObj-&gt;get_history();
print_r($fsqUsrHistory-&gt;response);
</pre>
</div>
<p></p>
<p>We first set a unique variable <strong>$fsqUsrHistory</strong> to hold our history object. This should return an array containing specific information about each of the previous checkins from the target account. The PHP <code>print_r()</code> function will display the response array in plaintext on the screen.</p>
<h2>Conclusion</h2>
<p>This has been a basic look into getting started with Foursquare&#8217;s API. OAuth is a very powerful open protocol for connecting into larger social communities. It offers the most secure solutions for both users and webmasters.</p>
<p>If you&#8217;re still a bit lost that&#8217;s to be expected at first. Try downloading the package from Github and messing around with some of the files. Even rudimentary PHP skills will suffice when working with these libraries, so it&#8217;s difficult to mess things up.</p>
<p>The <a href="http://developer.foursquare.com/">Foursquare Developers center</a> is an amazing tool for newbies. Scour the resources and look up and function calls you may be interested in working with. Similarly the official <a href="http://oauth.net/">OAuth website</a> has some terrific readings outlining the purpose and practical solutions for the protocol.</p>
]]></content:encoded>
			<wfw:commentRss>http://designshack.net/articles/php-articles/accessing-foursquare-api-data-via-oauth-and-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Developing Over the Dribbble API With PHP</title>
		<link>http://designshack.net/articles/php-articles/developing-over-the-dribbble-api-with-php/</link>
		<comments>http://designshack.net/articles/php-articles/developing-over-the-dribbble-api-with-php/#comments</comments>
		<pubDate>Mon, 21 Mar 2011 18:39:57 +0000</pubDate>
		<dc:creator>Jake Rocheleau</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://designshack.co.uk/?p=17793</guid>
		<description><![CDATA[Dribbble is the design networking app sweeping the digital revolution by storm. This collection of creative individuals offer pictures, or &#8220;shots&#8221;, of their latest works and share criticism through the rest of the community. It&#8217;s a great learning environment for studying web designers, illustrators, and icon makers. The community has been intricately planned and opened [...]]]></description>
			<content:encoded><![CDATA[<p>Dribbble is the design networking app sweeping the digital revolution by storm. This collection of creative individuals offer pictures, or &#8220;shots&#8221;, of their latest works and share criticism through the rest of the community. It&#8217;s a great learning environment for studying web designers, illustrators, and icon makers.</p>
<p>The community has been intricately planned and opened with an API. This stands for Application Programming Interface and would dramatically widen the range of adaptation to anybody who can understand the framework.</p>
<p><span id="more-17793"></span></p>
<div class="tutorialimage"><a href="http://www.flickr.com/photos/hellogeri/5320790523/"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/dribbble-cookies.jpg" alt="screenshot" width="510"/></a></div>
<p>In this brief tutorial I&#8217;ll be going over some of the basic concepts for developing over the Dribbble API. We will be focusing on PHP as the main programming language. This is causal to the fact PHP is supported in nearly every environment and it offers a very small learning curve for newbie developers.</p>
<h2>Examining the API</h2>
<p>In all truth It&#8217;s not a difficult sight to go through. In fact, <a href="http://dribbble.com/api">Dribbble&#8217;s official documentation</a> offers some great constructs into the powerful network of design shots.</p>
<p>When preparing for this type of project it&#8217;s key to consider a wrapper or library to work around. This saves us a load of time since we don&#8217;t need to re-create the most common functionality found within the API. The various libraries openly available through <strong>GitHub</strong> is another reason why PHP makes a great choice for the backend dev language.</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/api-documentation-beta.jpg" alt="screenshot" width="510"/></div>
<p>For our example we can go through <a href="https://github.com/martinbean/dribbble-php">Martin Bean&#8217;s Dribbble wrapper</a> to find exactly what we&#8217;re trying to do. You should download the most recent version and extract all the files into an open directory. Inside you&#8217;ll find two folders <strong>examples</strong> and <strong>src</strong>. It may be straightforward but &#8220;examples&#8221; will contain a few common implementations for function calls while &#8220;src&#8221; includes all of the files we need for a new project.</p>
<h2>Working with our PHP Wrapper</h2>
<p>Now that we&#8217;ve got everything settled we should create our application directory. At this point you&#8217;ll want to create a new project directory either on a locally installed version of Apache or in a remote web server.</p>
<p>WAMP and MAMP are still today the best options for all-in-one install packages. These include Apache as a web server with PHP/MySQL support for backend functionality. Once installed you&#8217;ll be working within a root www directory &#8211; this should hold all your server files.</p>
<p>I&#8217;ve created a new folder titled &#8220;dribble1&#8243; and moved the entire &#8220;src&#8221; folder into this. Also inside of our &#8220;dribble1&#8243; folder I&#8217;ve created a new file <code>index.php</code> which we can use to store all of our example code. You can structure your hierarchy in any way you&#8217;d like, just be sure all the src files are kept together and can be accessed from your project.</p>
<h2>Including Headers</h2>
<p>At the very top of your new index.php file it&#8217;s important to include the following code bits. These are used to create a connection into the wrapper package we downloaded.</p>
<div style="overflow: auto; background-color: #eeeeee;"</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?php
&nbsp;
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'src/dribbble.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dribbble</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Dribbble<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
?<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span></pre></div></div>

</div>
<p></ br></p>
<p>Above we first require the core dribbble file from our wrapper&#8217;s src. Then we declare a <code>$dribbble</code> variable and set this to a new instance of our dribbble class. This is just some basic OOP in PHP, very basic constructs. Don&#8217;t fret too much if you don&#8217;t understand the syntax used.</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/tshirt-dribbble-graphic.jpg" alt="screenshot" width="510"/></div>
<p>Once this is included we have access to manipulate almost any area of the site at whim. There are many pre-defined functions written which allow us to pull comments, users, shots, and tons of other data.</p>
<p>As a basic example let&#8217;s look at a single user feed. This is possibly the most in-demand technique and does not require very much code. The inclusion of your most recent Dribbble shots into your portfolio page offers a level of professionalism which just can&#8217;t be matched with Twitter.</p>
<h2>Pulling Single User Data</h2>
<p>The code below is an example pulling data from a sample user. In this case we&#8217;re working with <a href="http://dribbble.com/alexanderustinov">alexanderustinov</a> who creates many icon sets and user interfaces.</p>
<div style="overflow: auto; background-color: #eeeeee;"</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>ul <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;shots&quot;</span><span style="color: #339933;">&gt;</span>  
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?php  
    <span style="color: #000088;">$player</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dribbble</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">player</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'alexanderustinov'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
    <span style="color: #000088;">$shots</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$player</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">shots</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'per_page'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$shots</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">shots</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$shot</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
        <span style="color: #b1b100;">echo</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&amp;lt;li&gt;&amp;lt;a href=&quot;%s&quot;&gt;&amp;lt;img src=&quot;%s&quot; alt=&quot;%s&quot; /&gt;&amp;lt;/a&gt;&amp;lt;/li&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$shot</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$shot</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">image_url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$shot</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</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;">?&gt;</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;/</span>ul<span style="color: #339933;">&gt;</span></pre></div></div>

</div>
<p></ br></p>
<p>The shots should be called as above with <code>$shot->image_url</code>. This is the default 400&#215;300 image size seen on all shots from the network. The API also offers access to a teaser image which is used in profile pages and search results. To use this replace <code>image_url</code> with <code>image_teaser_url</code> in the same context.</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/profile-shots-alexanderustinov.jpg" alt="screenshot" width="510"/></div>
<p>Above we are looping through each shot entry found for the given username. The only reason we can quickly cycle through these is because of the wrapper class we included earlier in our page. We set the <em>per_page</em> variable to 5 meaning once we hit 5 shots we don&#8217;t want to pull any more from the database. This number could be updated to any value you&#8217;d like.</p>
<h2>Displaying In-Page Shots Comments</h2>
<p>Known little to outsiders of the community many of the popular shots in Dribbble have constructive feedback and some interesting page comments. This is one of the best ways designers can share their ideas and criticisms with others in a pleasant way.</p>
<p>The following code could also be placed anywhere in your <code>index.php</code> file below the wrapper include. It will pull comments from a generated shot ID and display 5 from the first page.</p>
<div style="overflow: auto; background-color: #eeeeee;"</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$shot</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dribbble</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">shot</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">62170</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #000088;">$comments</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$shot</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comments</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'page'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'per_page'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
&nbsp;
    <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?php <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$comments</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>  
&nbsp;
        <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>ul <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;comments&quot;</span><span style="color: #339933;">&gt;</span>  
        <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?php <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$comments</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$comment</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>  
            <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>li<span style="color: #339933;">&gt;</span>  
                <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>p <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #339933;">&gt;&amp;</span>lt<span style="color: #339933;">;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&amp;lt;?php echo <span style="color: #006699; font-weight: bold;">$comment-&gt;player</span>-&gt;url; ?&gt;&quot;</span><span style="color: #339933;">&gt;&amp;</span>lt<span style="color: #339933;">;</span>?php <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">player</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;/</span>a<span style="color: #339933;">&gt;</span> says<span style="color: #339933;">:</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?php <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">body</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;/</span>p<span style="color: #339933;">&gt;</span>  
            <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;/</span>li<span style="color: #339933;">&gt;</span>  
        <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?php <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>  
    <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;/</span>ul<span style="color: #339933;">&gt;</span>  
&nbsp;
    <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?php <span style="color: #b1b100;">else</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>  
        <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>p<span style="color: #339933;">&gt;</span>Whoops<span style="color: #339933;">!</span> No comments found here<span style="color: #339933;">,</span> sure is quiet<span style="color: #339933;">...&amp;</span>lt<span style="color: #339933;">;/</span>p<span style="color: #339933;">&gt;</span>  
    <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?php <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</div>
<p></ br></p>
<p>The <code>$shot</code> and <code>$comments</code> variables are used to hold initial values for the loop. <code>$shot</code> is the ID and connector into which specific shot we&#8217;re targeting, while <code>$comments</code> holds an array containing data about each pulled comment.</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/dribbble-icon-responses.jpg" alt="screenshot" width="510"/></div>
<p>Afterwards we confront some simple if/else logic checking if any comments could be pulled. If so we create another <strong>foreach loop</strong> to extract the data into a set of paragraphs inside an unordered list. All of the page HTML is easily malleable and shouldn&#8217;t take long for even novice web developers to pick up on.</p>
<h2>Conclusion</h2>
<p>This has been a brief introduction into developing over PHP and Dribble&#8217;s API. The network is constantly growing and features some of the best design work found from around the web. Thanks to <a href="https://github.com/martinbean">Martin Bean</a> and his Github repos to expedite the process.</p>
<p>If you&#8217;re lost I&#8217;d recommend checking out a few PHP tutorials on Google. There are options for beginners to dive into classes and Object-Oriented constructs. I&#8217;d also suggest our <a href="http://designshack.net/articles/css/30-free-video-tutorials-for-learning-web-design">list of video tutorials in web design</a> which covers a cascade of topics including markup and project development.</p>
]]></content:encoded>
			<wfw:commentRss>http://designshack.net/articles/php-articles/developing-over-the-dribbble-api-with-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>27 Utilities for Saving and Sharing Code Snippets</title>
		<link>http://designshack.net/articles/css/27-utilities-for-saving-and-sharing-code-snippets/</link>
		<comments>http://designshack.net/articles/css/27-utilities-for-saving-and-sharing-code-snippets/#comments</comments>
		<pubDate>Fri, 30 Jul 2010 18:11:37 +0000</pubDate>
		<dc:creator>Joshua Johnson</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://designshack.co.uk/?p=10877</guid>
		<description><![CDATA[You&#8217;ve got countless bits of HTML5, CSS3, JavaScript, and PHP rolling around in your head and on your hard drive that you are forced to try to recall each time you start a project. It&#8217;s time to get organized. This post will briefly point you to 27 great utilities that not only help you store [...]]]></description>
			<content:encoded><![CDATA[<p>You&#8217;ve got countless bits of HTML5, CSS3, JavaScript, and PHP rolling around in your head and on your hard drive that you are forced to try to recall each time you start a project. It&#8217;s time to get organized.</p>
<p>This post will briefly point you to 27 great utilities that not only help you store your favorite snippets both locally and in the cloud, but also make sharing with others a breeze. Enjoy!</p>
<p><span id="more-10877"></span></p>
<h2>Full-Scale Storing and Sharing</h2>
<p>The sites below are a great way to build a personal code repository that you can access from anywhere and easily share with others. They&#8217;re also great sites to find helpful snippets in a number of languages.</p>
<p>I haven&#8217;t used them all in-depth but my preferred site is <a href="http://www.snipplr.com/">Snipplr</a>. I really like the interface, the huge library of snippets, the versioning options, etc. They&#8217;re definitely all worth checking out though.</p>
<h3><a href="http://www.snipplr.com/">Snipplr</a></h3>
<p>&#8220;With Snipplr you can keep all of your frequently used code snippets in one place that&#8217;s accessible from any computer. You can share your code with other visitors and use what they post, too. Did we mention that Snipplr works with TextMate? Yeah, we rock.&#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-2.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://codesnippets.joyent.com/">CodeSnippets</a></h3>
<p>&#8220;Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)&#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-5.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://www.bytemycode.com/">Home | byteMyCode</a></h3>
<p>&#8220;byteMyCode is a project designed to make the task of sharing source code easier. We have modeled a community around making it easy to find, share, revise, comment on, and rate code snippets.&#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-6.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://snippetsmania.com/">SnippetsMania</a></h3>
<p>&#8220;Organize and share your code snippets. Snippetsmania is a free accessible collection of code snippets. It currently supports 34 different programming languages. The database holds a total count of 99 code and programming snippets and examples.&#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-7.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://codesnipp.it/">codesnipp.it</a></h3>
<p>&#8220;codesnipp.it is a social site for developers to post their snippets, follow and share code with other devs and get coding help. &#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-8.jpg" alt="screenshot" width="510"/></div>
<h2>Paste and Share Quickly</h2>
<p>The apps below are similar to those we just looked at but place much more emphasis on fast code sharing as opposed to organized code storage. Most just require a copy and paste and you&#8217;re ready to go!</p>
<h3><a href="http://snipt.org/">Snipt.org</a></h3>
<p>Share source code on Twitter and the Web.</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-1.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://pastie.org/">New &#8211; Pastie</a></h3>
<p>&#8220;Clean user interface with sensible defaults. Need to nickname, describe, set tab settings and more? You&#8217;ll have to look elsewhere. One text box, paste, done. Simple.&#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-3.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://www.smipple.net/">Smipple &#8211; Social Code Snippets</a></h3>
<p>&#8220;Smipple is a social service for saving, storing, organizing, and sharing snippets of code with your friends and coworkers. Make friends. Improve your coding skills. Earn bragging rights.&#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-4.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://gist.github.com/">Gist &#8211; GitHub</a></h3>
<p>&#8220;Gist is a simple way to share snippets and pastes with others. All gists are git repositories, so they are automatically versioned, forkable and usable as a git repository.&#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-9.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://www.copypastecode.com/">Copy Paste Code</a></h3>
<p>&#8220;Copy Paste Code allows you to publish your code with colored syntax for most popular programming languages out there. After publishing you can share your code to colleagues and friends, and they can debug and use your code snippets.&#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-10.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://codetidy.com/">CodeTidy</a></h3>
<p>A quick and easy, no-frills code sharing utility. </p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-11.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://ideone.com/">Ideone</a></h3>
<p>&#8220;Ideone is something more than a pastebin; it&#8217;s an online compiler and debugging tool which allows<br />
to compile and run code online in more than 40 programming languages.&#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-12.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://pastebin.com/">Pastebin</a></h3>
<p>One of the most popular tools of this kind, Pastebin supports a huge list of languages.</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-13.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://mysticpaste.com/new">Mystic Paste</a></h3>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-14.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://pastesite.com/">PasteSite.Com &#8211; Easy Usable Pastebin</a></h3>
<p>&#8220;PasteSite.Com is a tool which allows you to submit a sample of code which can then be viewed with syntax highlighting from the website.&#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-15.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://slexy.org/">Slexy 2.0</a></h3>
<p>&#8220;Slexy.org is a powerful, slick, and sexy pastebin designed with the user in mind. Use Slexy.org to paste errors for debugging, show off your code or your tech specs, or share anything else with other Internet users worldwide.&#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-16.jpg" alt="screenshot" width="510"/></div>
<h2>Mac Applications</h2>
<p>In my search for online snippet sharing tools I came across a number of Mac applications that are great for building code libraries so I figured I&#8217;d toss them in as well. Check out jCodeCollector and Snippely first if you&#8217;re looking for a solid free option.</p>
<h3><a href="http://fuelcollective.com/snippet/">Snippet: $12.95</a></h3>
<p>&#8220;Easily create Snippets from selected text anywhere.&#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-17.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://www.snippetsapp.com/">Snippets: $39.95</a></h3>
<p>&#8220;Snippets is a powerful application for Mac OS X that stores the most valuable pieces of code you can reuse in different projects many times.&#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-18.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://code.google.com/p/snippely/">Snippely: Free</a></h3>
<p>&#8220;Snippely is a basic text and code organizational tool. Instead of storing bits of code, quick notes, and memos in text files all over your hard drive, this application will let you save and organize &#8220;snippets&#8221; in one convenient location. A snippet is a collection of one or more pieces of code and notes. Snippets are stored in groups for organization and quick retrieval.&#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-19.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://www.macupdate.com/info.php/id/27798/jcodecollector">jCodeCollector: Free</a></h3>
<p>&#8220;jCodeCollector is an application that helps you to manage your code snippets. Using jCodeCollector you can: store, tag and comment your snippets, find quickly the code you need.&#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-20.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://www.mcubedsw.com/software/codecollectorpro">Code Collector Pro:  €20</a></h3>
<p>&#8220;When you spend all day writing code, it is only natural to start having feelings of déjà vu. Didn&#8217;t you write that same few lines of code a few days ago, and again a few days before that? Why are you wasting time re-writing that code again and again? Code Collector Pro is the answer, giving you the ability to store snippets of code and retrieve them with a few keystrokes&#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-21.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://myownapp.com/applications/mysnippets/mysnippets.html">MySnippets: $14.95</a></h3>
<p>&#8220;All of your snippets, the files you need, drafts, parts of a text, CSS templates and anything else you still need in order to deliver outstanding results: All of that is now just one single click, one single keystroke away. &#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-22.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://droplr.com/hello">Droplr: Free</a></h3>
<p>Unlike the apps above, Droplr is not the ideal place to store code snippets. However, among other things, it is a super easy way to share snippets of code. Just select some text and drag it to your menu bar. A URL will automatically be placed in your clipboard for you to paste in anywhere you want.</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-23.jpg" alt="screenshot" width="510"/></div>
<h2>Text Expanders</h2>
<p>One special breed of snippet applications is those that allow you to type a few characters which then automatically expand into a larger chunk of text. I&#8217;ve recently started using TextEpander and it has completely revolutionized the way I work. Now I can&#8217;t imagine trying to code without it!</p>
<p>I&#8217;ve searched for hours in vain in an attempt to find a free text expanding utility, leave a comment if you know of one.</p>
<h3><a href="http://www.smileonmymac.com/TextExpander/">TextExpander: $34.95 (Mac)</a></h3>
<p>&#8220;TextExpander saves you countless keystrokes with customized abbreviations for your frequently-used text strings and images.&#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-24.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://www.ergonis.com/products/typinator/">Typinator: €19.99 (Mac)</a></h3>
<p>&#8220;Typinator boosts your productivity and eliminates errors by automating the process of inserting frequently used text and graphics and auto-correcting typing errors.&#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-25.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://www.nch.com.au/fastfox/index.html">FastFox: $34.99 (Windows &#038; Mac)</a></h3>
<p>&#8220;FastFox is an easy to use text expander that can create and store keyboard shortcuts that expand to an entire phrase, paragraph or document.&#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-26.jpg" alt="screenshot" width="510"/></div>
<h3><a href="http://www.gideonsoftworks.com/snippetmonkey.html">SnippetMonkey</a></h3>
<p>&#8220;Snippet Monkey saves you countless hours monkeying around by allowing you to create shortcuts for phrases you frequently type. We call the phrases snippets and Snippet Monkey allows you to create new snippets from the clipboard or via our easy to use snippet editor.&#8221;</p>
<div class="tutorialimage"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/ds-snippets-27.jpg" alt="screenshot" width="510"/></div>
<h2>What Are Your Favorites?</h2>
<p>There are a lot of utilities here to choose from and I would love to hear your feedback on those that you&#8217;ve tried. For my own purposes I use TextExpander to quickly insert code, Snipplr to store random bits long-term, and Droplr to quickly share snippets. What combination will fits your workflow best?</p>
]]></content:encoded>
			<wfw:commentRss>http://designshack.net/articles/css/27-utilities-for-saving-and-sharing-code-snippets/feed/</wfw:commentRss>
		<slash:comments>40</slash:comments>
		</item>
		<item>
		<title>Why You Should Build a Front End Package</title>
		<link>http://designshack.net/articles/css/why-you-should-build-a-front-end-package/</link>
		<comments>http://designshack.net/articles/css/why-you-should-build-a-front-end-package/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 14:41:41 +0000</pubDate>
		<dc:creator>Joshua Johnson</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Standards]]></category>

		<guid isPermaLink="false">http://designshack.co.uk/?p=5680</guid>
		<description><![CDATA[How do you begin building a website? The majority of developers probably start from scratch or pull in a few resources from previous sites. The more organized among us have developed a custom toolbox from which to begin a site that proves to be an essential part of their workflow. Today we&#8217;ll discuss why you [...]]]></description>
			<content:encoded><![CDATA[<p class="lead">How do you begin building a website?</p>
<p>The majority of developers probably start from scratch or pull in a few resources from previous sites. The more organized among us have developed a custom toolbox from which to begin a site that proves to be an essential part of their workflow. </p>
<p>Today we&#8217;ll discuss why you should consider building your own front end package to serve as a starting point for every single site you create.</p>
<p><span id="more-5680"></span></p>
<h2>What Is a Front End Package?</h2>
<p>What I mean by front end package is essentially a set of tools and conventions that standardize certain elements of the web development process. The inspiration for this article comes from the creative minds at <a href="http://erskinedesign.com/">Erskine Design</a>. As a designer, you&#8217;re probably a visual thinker so we&#8217;ll cut straight to the diagram:</p>
<h4>The Basic Outline or Erskine&#8217;s Ultimate Front End Package</h4>
<div class="tutorialimage"><a href="http://erskinelabs.com/post/the-process-toolbox-part-seven-convention/"><img class="alignNone size-full wp-image-1611" src="http://designshack.net/wp-content/uploads/UP-outline.jpg" alt="screenshot" width="510"/></a></div>
<p>As you can see, Erskine has essentially built a basic framework from which to jump off for major web design projects. They sum it up as <strong><em>&#8220;a bumper compendium of cascading and connected CSS files, naming conventions, modules, plugins and library scripts that ensure any project led or worked on by any member(s) of the team will stay on convention, and be simpler for anyone else to step into and work with at any time.</em></strong>&#8221; </p>
<p>Possessing such a framework can be invaluable for a number of reasons, which we&#8217;ll discuss below. The argument posed by some is that such a framework or toolkit is not only helpful, but absolutely necessary. Erskine Design&#8217;s Simon Collison goes so far as to say &#8220;Without question or compromise, every website needs to be built with a solid foundation layer and an <em>Ultimate Package</em>.&#8221;</p>
<p>Let&#8217;s take a look at some of the benefits from, and reasons for, building your own custom front end package. (Based on some of the suggestions from <a href="http://www.slideshare.net/collylogic/developing-your-ultimate-package">Erskine&#8217;s Presentation, found here</a>)</p>
<h2>Elimination of Repetition</h2>
<p>This is the most basic and understandable reason for developing a front end package. Every time you start to build a site you go through several setup steps such as forming the bare HTML structure, creating the external CSS files, linking the HTML to the external CSS,  importing jQuery and/or any other JavaScript libraries you often use, etc. Developing a front end package recovers all of this lost time by making it extremely easy to start a new site: just copy the folder containing the framework and you&#8217;re up and running. </p>
<p>You might argue that these tasks don&#8217;t take a significant amount of time or are even necessary to get your brain into a web development mindset. To answer these arguments I would first suggest that you time yourself to see just how long you lose on every project getting your file hierarchy in place, setting up and loading scripts and styles, figuring out naming conventions, and fixing sloppy mistakes. I&#8217;ll wager it&#8217;s a lot more than you think. Finally, to the latter argument I would challenge you to retrain your brain to accept a new part of the process as the beginning. Try jumping right into experimentation with your system in place and discover how much nicer it is to skip all of the tedious, repetitive tasks.</p>
<h2>Standardization</h2>
<p>Standardization is a major benefit from using a prefab toolkit. Every time you start a new project, you might do things slightly different. This can be something big, like changing the way you layout your HTML, or something small, like deciding on a new naming convention. This can make it extremely difficult for others to follow your work or even for you to go back later and remember how you were doing things at the time.</p>
<p>As you develop your front end package, keep standardization at the forefront of your mind. Decide on the best way you know to do every little thing and stick with those conventions through every project you start. Markup your comments the same way, organize your CSS the same way, use the same variable naming conventions, utilize the same folder hierarchy, use the same CSS resets, etc. Taking all the little decisions and guesswork out of your system has the benefit of streamlining the entire development process to ensure you&#8217;re creating a discernible, organized site as quickly as possible.</p>
<p>This is not to say you should decide on a system and stick with it permanently. Let it evolve as you learn and discover better methods, just don&#8217;t integrate new methods flippantly or frequently enough to void the usefulness of the entire package. When you decide on a better way to do something, make sure you are absolutely certain that it will improve your system and be sure to make a note outlining the change and when it was integrated so you know what to expect from older projects. </p>
<h2>Better Collaboration</h2>
<p>This is where a front end package crosses over from &#8220;nice to have&#8221; to &#8220;absolutely essential.&#8221; When you&#8217;re working with a team of developers on a large project, one of the biggest inefficiencies you can have is a failure to get everyone on the same page from the outset of the project. </p>
<p>If you&#8217;ve got Bill structuring his part of the project one way, Jill structuring her part another way, and Will trying to keep up with both Bill&#8217;s and Jill&#8217;s methods, things are going to get messy fast (and not just because all of your employees&#8217; names happen to rhyme). This will inevitably lead to long meetings spent arguing over trifles. If you have team members that have already started a project using certain conventions, you can bet they&#8217;re going to defend that method to the death to avoid going back and fixing what they consider to be finished work. This is why it&#8217;s extremely important to develop a front end package in cases where significant collaboration is involved. You&#8217;ll probably still have to hold a meeting to decide on specific conventions to follow, but you&#8217;ll find that team members are much more flexible to new methods if it doesn&#8217;t require backtracking. </p>
<p>The key takeaway here is to develop a system before a project begins, not during. This will increase the chance of acceptance and prevent a lot of incompatibility issues down the road. Also, be sure to include your team in the decision making process. This is hugely important to the success of the package for a number of reasons. First, it&#8217;s always a bad idea for management to create a system to streamline a given task without consulting the people who are closest to that task. No matter how many more college degrees you have than the people under you, chances are they are the single best authority on what will and won&#8217;t work. Finally, apart from the issue of effectiveness is again the issue of acceptance. If you hand your team a set of guidelines that they&#8217;ve had no part in developing, they will drag their feet and complain the entire way because you are forcing them into something they don&#8217;t want to do. However, if you let team members from every level actively participate in the development of the conventions, they are much more likely to conform to the new system because they aided in its creation and direction.</p>
<h2>Quality Control</h2>
<p>Developing a front end package allows you to implement a certain degree of quality control across the members of your team from the outset of the project. It ensures common mistakes, such as grabbing the wrong doc type or forgetting to include a certain browser specific style sheet, aren&#8217;t made. Further, having a strict system in place can help prevent intentionally sloppy work. In a mad rush to get a project going, developers will often use non-standards compliant code, vague variable names, obscure tricks, and any number of other shortcuts with the argument that they will go back and fix these things later. The problem of course is that there usually isn&#8217;t time to go back and fix these things later in the project as you approach key deadlines. Many of these problems will disappear if your foster a culture that avoids such practices and discourages straying from agreed upon conventions.</p>
<h2>In Regard to Design &#038; Innovation</h2>
<p>Before I close and ask to hear your opinions, I want to preempt an argument that might arise. Many see common conventions and strict rules as something that will cripple the design process, virtually eliminating any room for creativity or innovation. This simply is not the case in this instance and is in fact the opposite result of what a well-designed front end package will provide.</p>
<p>A good front end package will actually allow you to focus more on the creative elements of the development process through standardization of areas that eat up time and whose variation would not making a significant difference in the end result. What I mean by this is that such elements as your folder hierarchy will go completely unnoticed by the end user and are therefore not the place to focus your creativity on every single project. The idea here is to get through the boring stuff in one fell swoop so that you can quickly dig into the things that do, and should, vary from site to site; the things that make each site unique. With this type of system in place, you can spend more time developing original user interfaces, choosing custom color schemes, trying different font-families and coding innovative functions.</p>
<p>If the system you come up with hinders the creative process, it simply isn&#8217;t doing its job and should therefore be scrapped in favor of a trip back to the drawing board. </p>
<h2>Free Resources</h2>
<p>Sold on the idea of developing your own front end package but don&#8217;t know where to begin? Here are some free resources to get you started.</p>
<ul>
<li><a href="http://perishablepress.com/press/2007/10/23/a-killer-collection-of-global-css-reset-styles/">A Killer Collection of Global CSS Reset Styles</a></li>
<li><a href="http://html.comsci.us/examples/blank.html">HTML Blank Page Examples</a></li>
<li><a href="http://digital-crocus.com/kb/43_A_Simple_PHP_Template_System.html">A Simple PHP Template System</a></li>
<li><a href="http://code.google.com/apis/ajaxlibs/">Google Code: Hosted JavaScript Libraries (jQuery, MooTools, etc)</a></li>
<li><a href="http://www.intensivstation.ch/en/templates/">16 Basic CSS Layout Templates</a></li>
</ul>
<h2>Speak Your Mind</h2>
<p>The above represents my long-winded argument for why I believe Erskine Design is right in asserting that every website should be built from a strong, standardized and preset foundation. Let us know if you think developing such a system is worth your time. Better yet, if you have a system in place, let us know how it works!</p>
]]></content:encoded>
			<wfw:commentRss>http://designshack.net/articles/css/why-you-should-build-a-front-end-package/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Smart File Type Detection Using PHP</title>
		<link>http://designshack.net/articles/php-articles/smart-file-type-detection-using-php/</link>
		<comments>http://designshack.net/articles/php-articles/smart-file-type-detection-using-php/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 08:59:51 +0000</pubDate>
		<dc:creator>Kishore Nallan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[detection]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.designshack.co.uk/?p=689</guid>
		<description><![CDATA[In most web applications today, there is a need to allow users to upload images, audio and video files. Sometimes, we also need to restrict certain types of files from being uploaded &#8211; an executable file being an obvious example. Security aside, one might also want to prevent users from misusing the upload facility, e.g. [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.designshack.net/wp-content/uploads/padlock.jpg" alt="padlock" title="padlock" width="200" height="200" class="right size-full wp-image-722" />In most web applications today, there is a need to allow  users to upload images, audio and video files. Sometimes, we also need to  restrict certain types of files from being uploaded &#8211; an executable file being an obvious example.</p>
<p>Security aside, one might also want to prevent users from misusing  the upload facility, e.g. uploading copyrighted music files illegally and using the service to promote piracy! In this article, we&#8217;ll look into a few ways in which we can achieve this.</p>
<p><span id="more-689"></span></p>
<h2>File  type detection using extension and MIME types</h2>
<p>I am not going to talk about this in too much detail as  after all, this is what we normally do when we want to restrict certain files.  We simply get the MIME type of the file using  <code>$_FILES['myFile']['type']</code> and check if it’s of a  valid type.</p>
<p>Or we might scan the last few characters of the file name  and reject files ending with a certain extension.  Unfortunately, these methods are hardly  sufficient, as one can easily change the extension of a file to bypass this  restriction. Furthermore, MIME type information is given by the browser and most browsers, if not all, determine the mime type based upon the  file&#8217;s extension! Hence MIME types can be pretty easily spoofed too.</p>
<p>Let&#8217;s now explore some others ways which offer better fool-proofness.</p>
<h2>Using Magic Bytes</h2>
<p>The best way to determine the file type is by examining the first few bytes of a file &#8211; referred to as &#8220;magic bytes&#8221;. Magic bytes are essentially signatures that vary in length between 2 to 40 bytes in the file headers, or at the end of a file. There are several hundred types of files, and quite a few of them have several file signatures associated with them. You can see a list of file signatures <a href="http://www.garykessler.net/library/file_sigs.html">over here</a>.</p>
<p>Although inconsistent, this is our best bet in detecting file types reliably. This seemingly difficult task has been made really easy by a PECL extension called <a href="http://pecl.php.net/package/Fileinfo">Fileinfo</a>. As of PHP 5.3, Fileinfo is shipped with the main distribution and is enabled by default, so this is definitely a robust and simple way to detect and impose restrictions on the types of files uploaded.</p>
<p>Let&#8217;s now see how we can detect a file type using Fileinfo:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/path/to/file&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// in PHP 4, we can do:</span>
<span style="color: #000088;">$fhandle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">finfo_open</span><span style="color: #009900;">&#40;</span>FILEINFO_MIME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mime_type</span> <span style="color: #339933;">=</span> <span style="color: #990000;">finfo_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fhandle</span><span style="color: #339933;">,</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// e.g. gives &quot;image/jpeg&quot; </span>
&nbsp;
<span style="color: #666666; font-style: italic;">// in PHP 5, we can do:</span>
&nbsp;
<span style="color: #000088;">$file_info</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> finfo<span style="color: #009900;">&#40;</span>FILEINFO_MIME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// object oriented approach!</span>
<span style="color: #000088;">$mime_type</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$file_info</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">buffer</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</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;">// e.g. gives &quot;image/jpeg&quot;</span>
&nbsp;
<span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mime_type</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;image/jpeg&quot;</span><span style="color: #339933;">:</span>
		<span style="color: #666666; font-style: italic;">// your actions go here...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Handling image uploads</h2>
<p>If you intend to allow only image uploads, then you can use  the inbuilt <code>getimagesize()</code> function to ensure that the user is actually uploading a valid image file. This functions returns false, if the file is not a valid image file.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//  Let's assume that the name attribute of the file input field you have used is &quot;myfile&quot;</span>
&nbsp;
<span style="color: #000088;">$tempFile</span> <span style="color: #339933;">=</span>  <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'myFile'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tmp_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// path of the temp file created by PHP during upload</span>
<span style="color: #000088;">$imginfo_array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tempFile</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">// returns a false if not a valid image file</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$imginfo_array</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$mime_type</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$imginfo_array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mime'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mime_type</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
&nbsp;
	<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;image/jpeg&quot;</span><span style="color: #339933;">:</span>
		<span style="color: #666666; font-style: italic;">// your actions go here...</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;This is not a valid image file&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Reading and interpreting magic bytes manually</h2>
<p>If for some reason, you are not able to install Fileinfo, then you can still manually determine the file type by reading the first few bytes of a file and comparing them with known magic bytes associated with the particular file type. This process definitely has an element of trial and error, because there is still a chance that there are a few undocumented magic bytes associated with legitimate file formats. As a result, valid files could be rejected by your system. However it&#8217;s not impossible as a couple of years back, I was asked to work on a script that allowed only genuine mp3 files to be uploaded, and since we could not use Fileinfo, we resorted to this manual scanning. It took me a  while to account for some of the undocumented magic bytes for mp3, but pretty soon, I got a stable upload script running.</p>
<p>Before I end, I would just like to part with a general word of caution: Make sure that you never call an <code>include()</code> with a file that was uploaded, as PHP  code can very well be hidden as part of the picture, and the picture would pass your tests for file validation just fine, only to cause havoc when executed by the server.</p>
]]></content:encoded>
			<wfw:commentRss>http://designshack.net/articles/php-articles/smart-file-type-detection-using-php/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Object Caching 446/507 objects using disk: basic

Served from: designshack.net @ 2012-02-10 05:07:28 -->
