<?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>Del Harvey - Developer &#187; PHP</title>
	<atom:link href="http://lotsofcode.com/tags/php/feed" rel="self" type="application/rss+xml" />
	<link>http://lotsofcode.com</link>
	<description>Lots of Code</description>
	<lastBuildDate>Thu, 27 Oct 2011 19:07:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>PHP Tag cloud</title>
		<link>http://lotsofcode.com/php/tag-cloud.htm</link>
		<comments>http://lotsofcode.com/php/tag-cloud.htm#comments</comments>
		<pubDate>Sun, 20 Feb 2011 18:46:08 +0000</pubDate>
		<dc:creator>lotsofcode</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[cloud]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[tags]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.lotsofcode.com/?p=142</guid>
		<description><![CDATA[Generate your own tag cloud with this easy to use PHP class. A tag cloud is typically used to describe the content of web sites. Many customisable features.]]></description>
			<content:encoded><![CDATA[<div class="demo-and-download"><a id="dad_demo_anchor" href="/demo/tag-cloud" target="_blank">Demo</a><a id="dad_github_anchor" href="https://github.com/lotsofcode/tag-cloud" target="_blank">Download</a></div><p>Generate your own tag cloud with this easy to use PHP class, simply include the class, instantiate a new object and then push your words into the cloud and voila, you have a tag cloud.</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
require_once 'classes/wordcloud.class.php';
$cloud = new wordCloud();
$cloud-&gt;addWord('php'); // Basic Method of Adding Keyword
$cloud-&gt;addWord(array('word' =&gt; 'google', 'url' =&gt; 'http://www.google.com')); // Advanced user method
$cloud-&gt;addString('List of words i would like to include');
$cloud-&gt;addWords('hello','world','how','are','foo','bar');
echo $cloud-&gt;showCloud();
?&gt;
</pre>
<p>The clouds default ordering is by random, if you wanted to order the cloud by size or word from largest to smallest, this can be done as follows.</p>
<pre class="brush: php; title: ; notranslate">
$cloud-&gt;orderBy('size','desc');
</pre>
<p>As mentioned above, you can arbitrarily add custom tags to pass through the tag cloud generator for use later on in your code. If you wanted to colour code the tags based on word, you could do the following.</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
$cloud = new wordCloud();
$cloud-&gt;addWord(array('word' =&gt; 'php', 'size' =&gt; 1, 'url' =&gt; 'http://www.php.net', 'colour' =&gt; '#FF0000'));
$cloud-&gt;addWord(array('word' =&gt; 'google', 'size' =&gt; 2, 'url' =&gt; 'http://www.google.com', 'colour' =&gt; '#00FF00'));
$cloud-&gt;addWord(array('word' =&gt; 'digg', 'size' =&gt; 3, 'url' =&gt; 'http://digg.com', 'colour' =&gt; '#0000FF'));
$cloud-&gt;addWord(array('word' =&gt; 'lotsofcode', 'size' =&gt; 4, 'url' =&gt; 'http://www.lotsofcode.com', 'colour' =&gt; '#FFFF00'));
$myCloud = $cloud-&gt;showCloud('array');
foreach ($myCloud as $cloudArray) {
  echo ' &amp;nbsp; &lt;a href=&quot;'.$cloudArray['url'].'&quot; style=&quot;color: '.$cloudArray['colour'].';&quot; class=&quot;word size'.$cloudArray['range'].'&quot;&gt;'.$cloudArray['word'].'&lt;/a&gt; &amp;nbsp;';
}
?&gt;
</pre>
<p>The above examples show the tag cloud wrapper is completly cusomisable and can be used in lots of different ways.</p>
<p>Not only can you generate a tag cloud, but you can customise the output if required to pass any information you like, such as href, link colours and more, check out the demo for more details on this.</p>
<p>Some of the features that are available are:<br />
    * Link and URL Assignment<br />
    * Additional attribute assignment e.g. title etc<br />
    * Add styles and colour options to each tag<br />
    * Set a limit to the amount of tags that are displayed<br />
    * String to cloud, parse a single string into a tag cloud instantly<br />
    * Ignore list, to remove particular words from the cloud. e.g. and, the, it, i<br />
    * Ordering by a selective field. e.g. word, size, colour<br />
    * Parse an argument seperated list of words<br />
    * add a string with a custom seperator<br />
    * format of word output. (Customise upper/lowercase words &amp; set trimming of words. Single word and multi-word customisation.)</p>
<p>Within the demo, i have provided:<br />
    * Examples of intergrating with a MySQL Database<br />
    * Examples of intergrating with a Flat Text File</p>
<p><strong>Tag cloud background info</strong></p>
<p>Originally i created a tag cloud script and wrote up a tutorial on how to create one yourself, this was quite well received and from there i developed a newer version of the code base with lots more features, because this wasn't very well publicised, the original page that sat here was visited on many occasions with the same questions being asked over again about bugs and features etc. So what i have done is divided the old pages into a seperate url so that they can all be referenced if needed but are all pointed to this article for the most up to date versions of the code.</p>
<p>You can view the original <a href="/php/tag-cloud-v1.htm" target="_blank">tag cloud tutorial here</a>.<br />
For legacy, here is the <a href="/php/tag-cloud-v2.htm" target="_blank">original v2 of the script here</a>.</p>
<p><strong>Bugs fixed</strong><br />
- setLimit didn't work. (20/02/11)<br />
- Removed words get removed if limit applied. (20/02/11)<br />
- Bracket conversion of percentage adjusted to show smaller sizes on less frequent words. (20/02/11)</p>
]]></content:encoded>
			<wfw:commentRss>http://lotsofcode.com/php/tag-cloud.htm/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>WordPress TinyMCE Image Upload Plugin</title>
		<link>http://lotsofcode.com/javascript-and-ajax/wordpress-tinymce-image-upload-plugin.htm</link>
		<comments>http://lotsofcode.com/javascript-and-ajax/wordpress-tinymce-image-upload-plugin.htm#comments</comments>
		<pubDate>Wed, 12 Mar 2008 22:01:39 +0000</pubDate>
		<dc:creator>lotsofcode</dc:creator>
				<category><![CDATA[Javascript & AJAX]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://www.lotsofcode.com/javascript-and-ajax/wordpress-tinymce-image-upload-plugin.htm</guid>
		<description><![CDATA[DemoDownloadAfter many hours looking and looking for a plugin for TinyMCE i finally found one that actually worked quite well and was what it said it is .. An actual image upload plugin for WordPress's TinyMCE ... ! Take a look here: http://www.soderlind.no/archives/2006/01/03/imagemanager-20/ Download here: http://www.soderlind.no/download/ImageManager2.41.zip I know someone will really appreciate this post as [...]]]></description>
			<content:encoded><![CDATA[<div class="demo-and-download"><a id="dad_demo_anchor" href="/demo/tag-cloud" target="_blank">Demo</a><a id="dad_github_anchor" href="https://github.com/lotsofcode/tag-cloud" target="_blank">Download</a></div><p>After many hours looking and looking for a plugin for TinyMCE i finally found one that actually worked quite well and was what it said it is ..  An actual image upload plugin for WordPress's TinyMCE ... !</p>
<p>Take a look here: <a href="http://www.soderlind.no/archives/2006/01/03/imagemanager-20/">http://www.soderlind.no/archives/2006/01/03/imagemanager-20/</a></p>
<p><a href="http://www.lotsofcode.com/wp-content/uploads/Untitled_1.gif" onclick="ps_imagemanager_popup(this.href,'Untitled_1.gif','1006','582');return false" onfocus="this.blur()"><img src="http://www.lotsofcode.com/wp-content/uploads/.thumbs/tmb_Untitled_1.gif" alt="Untitled_1.gif" title="Untitled_1.gif" border="0" height="96" width="166" /></a></p>
<p>Download here: <a href="http://www.soderlind.no/download/ImageManager2.41.zip">http://www.soderlind.no/download/ImageManager2.41.zip</a></p>
<p>I know someone will really appreciate this post as i would appreciate someone else posting a link to such a great resourse, if anyone knows of any other open source / free plugins for TinyMCE please post here.</p>
<p>If PHP safe_mode is enabled on server you may find problems with thumbnails ...</p>
]]></content:encoded>
			<wfw:commentRss>http://lotsofcode.com/javascript-and-ajax/wordpress-tinymce-image-upload-plugin.htm/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tag Cloud v2 (archived)</title>
		<link>http://lotsofcode.com/php/tag-cloud-v2.htm</link>
		<comments>http://lotsofcode.com/php/tag-cloud-v2.htm#comments</comments>
		<pubDate>Thu, 28 Feb 2008 13:14:47 +0000</pubDate>
		<dc:creator>lotsofcode</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[cloud]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[tags]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://localhost/projects/lotsofcode_2008/uncategorized/tag-cloud-v2.htm</guid>
		<description><![CDATA[DemoDownloadTag Cloud Script (Latest Version) can be found here, with more customizable features. Please check out the demo page for examples on how to use the new functions available in this version. Some of the new features included in the new tag cloud script are: Link and URL Assignment Additional attribute assignment e.g. title etc [...]]]></description>
			<content:encoded><![CDATA[<div class="demo-and-download"><a id="dad_demo_anchor" href="/demo/tag-cloud" target="_blank">Demo</a><a id="dad_github_anchor" href="https://github.com/lotsofcode/tag-cloud" target="_blank">Download</a></div><p><a href="http://www.lotsofcode.com/php/tag-cloud.htm" style="display:block;border:1px solid #ff0000;padding:6px; color: #ff0000;"><span style="font-weight: bold; text-decoration: underline;">Tag Cloud</span><span style="font-weight: bold; text-decoration: underline;"> Script (</span><span style="font-weight: bold; text-decoration: underline;">Latest Version)</span> can be found here, with more customizable features.</a></p>
<p><span>Please check out the demo page for examples on how to use the new functions available in this version.</span></p>
<p>Some of the new features included in the new tag cloud script are:</p>
<ul>
<li>Link and URL Assignment</li>
<li>Additional attribute assignment e.g. title etc</li>
<li>Add styles and colour options to each tag</li>
<li>Set a limit to the amount of tags that are displayed</li>
<li>Examples of intergrating with a MySQL Database</li>
<li>Examples of intergrating with a Flat Text File</li>
<li>String to cloud, parse a single string into a tag cloud instantly</li>
<li>Ignore list, to remove particular words from the cloud. e.g. and, the, it, i</li>
<li>Ordering by a selective field. e.g. word, size, colour</li>
</ul>
<p><span style="font-weight: bold;">An example of a cloud be be easily generated, like so<br />
</span></p>
<p><code><?php<br />
$cloud = new wordCloud();<br />
$cloud->addWord('php'); // Basic Method of Adding Keyword<br />
$cloud->addWord(array('word' => 'google', 'size' => 2, 'url' => 'http://www.google.com')); // Advanced user method<br />
$cloud->addWord(array('word' => 'digg', 'url' => 'http://digg.com'));<br />
$cloud->addWord(array('word' => 'lotsofcode', 'size' => 4, 'url' => 'http://www.lotsofcode.com'));<br />
echo $cloud->showCloud();<br />
?><br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://lotsofcode.com/php/tag-cloud-v2.htm/feed</wfw:commentRss>
		<slash:comments>41</slash:comments>
		</item>
		<item>
		<title>How to write a tag cloud in PHP &#8211; Tag cloud tutorial</title>
		<link>http://lotsofcode.com/php/tag-cloud-v1.htm</link>
		<comments>http://lotsofcode.com/php/tag-cloud-v1.htm#comments</comments>
		<pubDate>Wed, 27 Feb 2008 19:12:43 +0000</pubDate>
		<dc:creator>lotsofcode</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[cloud]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[tags]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://localhost/projects/lotsofcode_2008/uncategorized/tag-cloud.htm</guid>
		<description><![CDATA[Learn how to write a tag cloud script to generate a tag cloud based on repetition of words. This tutorial will guide you through step by step and a download is available.]]></description>
			<content:encoded><![CDATA[<div class="demo-and-download"><a id="dad_demo_anchor" href="/demo/tag-cloud" target="_blank">Demo</a><a id="dad_github_anchor" href="https://github.com/lotsofcode/tag-cloud" target="_blank">Download</a></div><p><span></p>
<p><a href="http://www.lotsofcode.com/php/tag-cloud.htm" style="display:block;border:1px solid #ff0000;padding:6px; color: #ff0000;"><span style="font-weight: bold; text-decoration: underline;">Tag Cloud</span><span style="font-weight: bold; text-decoration: underline;"> Script (</span><span style="font-weight: bold; text-decoration: underline;">Latest Version)</span> can be found here, with more customizable features.</a></p>
<p><span style="font-weight: bold; text-decoration: underline;"><br />
Introduction</span><br />
<span style="font-weight: bold; text-decoration: underline;"><br />
</span>In this tutorial i am going to show you how to create a basic word / tag cloud using php and utilizing php classes for easy inclusion. I am going to create a class based cloud, this is because it will be more convenient for you to adapt it on your own website(s), if you don't know much about class based programming then click here to take a look at the class tutorial.<br />
<span style="font-weight: bold; text-decoration: underline;"><br />
Tutorial</span></span></p>
<p>First we need to create the class and for this example i am going to call the class 223wordCloud224 this is just my personal naming preference, feel free to call your cloud whatever you like, we are also going to assign the first class variable which will be the container of all the items in the array, we will call it 223wordsArray224.</p>
<blockquote>
<p class="code">&lt;?<br />
class wordcloud<br />
{<br />
var $wordsArray = array();<br />
}<br />
?&gt;</p>
</blockquote>
<p>Next, we need to assign the first function, because of the difference between PHP 4 and PHP 5 Constructors, i have created a method (function) called 223wordCloud224 and 223__construct224, when wordCloud is called it simultaneously call the PHP 5 constructor, so it's compatible with both versions like so.</p>
<blockquote>
<p class="code">&lt;?<br />
function __construct($words = false)<br />
{<br />
if ($words !== false &amp;&amp; is_array($words))<br />
{<br />
foreach ($words as $key =&gt; $value)<br />
{<br />
$this-&gt;addWord($value);<br />
}<br />
}<br />
}</p>
<p>function wordCloud($words = false)<br />
{<br />
$this-&gt;__construct($words);<br />
}<br />
?&gt;</p></blockquote>
<p>The main constructor basically can be used to load the words when the class object is created.</p>
<p>Now, we need to create a function to add words into the array as and when we need them.</p>
<blockquote>
<p class="code">&lt;?<br />
function addWord($word, $value = 1)<br />
{<br />
$word = strtolower($word);<br />
if (array_key_exists($word, $this-&gt;wordsArray))<br />
$this-&gt;wordsArray[$word] += $value;<br />
else<br />
$this-&gt;wordsArray[$word] = $value;</p>
<p>return $this-&gt;wordsArray[$word];<br />
}<br />
?&gt;</p></blockquote>
<p>The method above will basically check the array of words and if the word exists it will increase the repetition value, this can also be adjusted by manual input.</p>
<p>Next, we need to be able to get the size of the array, now because we have an array with values, we can use the values of all the items in the array added together like so.</p>
<blockquote>
<p class="code">&lt;?<br />
function getCloudSize()<br />
{<br />
return array_sum($this-&gt;wordsArray);<br />
}<br />
?&gt;</p>
</blockquote>
<p>Next, we need to create a function to calculate the class / range that each specific word should be assigned to, this will be done by using the percentage that will be returned from the final function.</p>
<blockquote>
<p class="code">&lt;?<br />
function getClassFromPercent($percent)<br />
{<br />
if ($percent &gt;= 99)<br />
$class = 1;<br />
else if ($percent &gt;= 70)<br />
$class = 2;<br />
else if ($percent &gt;= 60)<br />
$class = 3;<br />
else if ($percent &gt;= 50)<br />
$class = 4;<br />
else if ($percent &gt;= 40)<br />
$class = 5;<br />
else if ($percent &gt;= 30)<br />
$class = 6;<br />
else if ($percent &gt;= 20)<br />
$class = 7;<br />
else if ($percent &gt;= 10)<br />
$class = 8;<br />
else if ($percent &gt;= 5)<br />
$class = 9;<br />
else<br />
$class = 0;</p>
<p>return $class;<br />
}<br />
?&gt;</p></blockquote>
<p>Next we create a function that will shuffle all the values in the cloud, this is so that we can have a different output each time, this could be adapted so that it only shuffles the cloud if choose to.</p>
<blockquote>
<p class="code">&lt;?<br />
function shuffleCloud()<br />
{<br />
$keys = array_keys($this-&gt;wordsArray);</p>
<p>shuffle($keys);</p>
<p>if (count($keys) &amp;&amp; is_array($keys))<br />
{<br />
$tmpArray = $this-&gt;wordsArray;<br />
$this-&gt;wordsArray = array();<br />
foreach ($keys as $key =&gt; $value)<br />
$this-&gt;wordsArray[$value] = $tmpArray[$value];<br />
}<br />
}<br />
?&gt;</p></blockquote>
<p>Now, we need to create a function to display the words, i have decided to make this function have two possible outputs, this is because if you would like more control over the output or if you would like to use less standard class names then you can do so, the two possible output types are an php array and html spans.</p>
<blockquote>
<p class="code">function showCloud($returnType = "html")<br />
{<br />
$this-&gt;shuffleCloud();<br />
$this-&gt;fullCloudSize = $this-&gt;getCloudSize();<br />
$this-&gt;max = max($this-&gt;wordsArray);</p>
<p>if (is_array($this-&gt;wordsArray))<br />
{<br />
$return = ($returnType == "html" ? "" : ($returnType == "array" ? array() : ""));<br />
foreach ($this-&gt;wordsArray as $word =&gt; $popularity)<br />
{<br />
$sizeRange = $this-&gt;getClassFromPercent(($popularity / $this-&gt;max) * 100);<br />
if ($returnType == "array")<br />
{<br />
$return[$word]['word'] = $word;<br />
$return[$word]['sizeRange'] = $sizeRange;<br />
if ($currentColour)<br />
$return[$word]['randomColour'] = $currentColour;<br />
}<br />
else if ($returnType == "html")<br />
{<br />
$return .= "&lt;span class='word size{$sizeRange}'&gt;  {$word}   &lt;/span&gt;";<br />
}<br />
}</p>
<p>return $return;<br />
}<br />
}</p></blockquote>
<p>Ok, so now we have the class, we need to use this to create our cloud, this could be done like so.</p>
<blockquote>
<p class="code">&lt;?</p>
<p>// You could add items to the class by passing them through the constructor</p>
<p>$randomWords = array("webmasterworld", "Computer", "Skateboarding", "PC", "music", "music", "music", "music", "PHP", "C", "XHTML", "programming", "forums", "Chill out", "email", "forums", "Computer", "GTA", "Freetimers", "css", "mysql", "sql", "css", "mysql", "sql", "forums", "internet", "class", "object", "method", "music", "music", "music", "music", "gui", "encryption");</p>
<p>$cloud = new wordCloud($randomWords);</p>
<p>// Or you could assign them manually<br />
$cloud-&gt;addWord("harvey", 5);<br />
$cloud-&gt;addWord("music", 12);<br />
echo $cloud-&gt;showCloud();<br />
?&gt;</p></blockquote>
<p>Don't forget to create your styles to display the different size fonts, this is a little something i have come up with.</p>
<blockquote>
<p class="code">&lt;style&gt;<br />
&lt;!--<br />
.word {<br />
font-family: Tahoma;<br />
padding: 4px 4px 4px 4px;<br />
letter-spacing: 3px;<br />
}<br />
span.size1 {<br />
color: #000;<br />
font-size: 2.4em;<br />
}<br />
span.size2 {<br />
color: #333;<br />
font-size:2.2em;<br />
}<br />
span.size3 {<br />
color: #666;<br />
font-size: 2.0em;<br />
}<br />
span.size4 {<br />
color: #999;<br />
font-size: 1.0em;<br />
}<br />
span.size5 {<br />
color: #aaa;<br />
font-size: 1.6em;<br />
}<br />
span.size6 {<br />
color: #bbb;<br />
font-size: 1.4em;<br />
}<br />
span.size7 {<br />
color: #ccc;<br />
font-size: 1.2em;<br />
}<br />
span.size8 {<br />
color: #ddd;<br />
font-size: .8em;<br />
}<br />
//--&gt;<br />
&lt;/style&gt;</p>
</blockquote>
<p>To finish off i will finish with the code in full.</p>
<p><span style="font-weight: bold; text-decoration: underline;">Full PHP Tag Cloud Class</span></p>
<blockquote>
<p class="code">&lt;?<br />
/*<br />
@wordCloud<br />
Author: Derek Harvey<br />
Website: www.lotsofcode.com</p>
<p>@Description<br />
PHP Tag Cloud Class, a nice and simple way to create a php tag cloud, a database and non-database solution.<br />
*/</p>
<p>class wordCloud<br />
{<br />
var $wordsArray = array();</p>
<p>/*<br />
* PHP 5 Constructor<br />
*<br />
* @param array $words<br />
* @return void<br />
*/</p>
<p>function __construct($words = false)<br />
{<br />
if ($words !== false &amp;&amp; is_array($words))<br />
{<br />
foreach ($words as $key =&gt; $value)<br />
{<br />
$this-&gt;addWord($value);<br />
}<br />
}<br />
}</p>
<p>/*<br />
* PHP 4 Constructor<br />
*<br />
* @param array $words<br />
* @return void<br />
*/</p>
<p>function wordCloud($words = false)<br />
{<br />
$this-&gt;__construct($words);<br />
}</p>
<p>/*<br />
* Assign word to array<br />
*<br />
* @param string $word<br />
* @return string<br />
*/</p>
<p>function addWord($word, $value = 1)<br />
{<br />
$word = strtolower($word);<br />
if (array_key_exists($word, $this-&gt;wordsArray))<br />
$this-&gt;wordsArray[$word] += $value;<br />
else<br />
$this-&gt;wordsArray[$word] = $value;</p>
<p>return $this-&gt;wordsArray[$word];<br />
}</p>
<p>/*<br />
* Shuffle associated names in array<br />
*/</p>
<p>function shuffleCloud()<br />
{<br />
$keys = array_keys($this-&gt;wordsArray);</p>
<p>shuffle($keys);</p>
<p>if (count($keys) &amp;&amp; is_array($keys))<br />
{<br />
$tmpArray = $this-&gt;wordsArray;<br />
$this-&gt;wordsArray = array();<br />
foreach ($keys as $key =&gt; $value)<br />
$this-&gt;wordsArray[$value] = $tmpArray[$value];<br />
}<br />
}</p>
<p>/*<br />
* Calculate size of words array<br />
*/</p>
<p>function getCloudSize()<br />
{<br />
return array_sum($this-&gt;wordsArray);<br />
}</p>
<p>/*<br />
* Get the class range using a percentage<br />
*<br />
* @returns int $class<br />
*/</p>
<p>function getClassFromPercent($percent)<br />
{<br />
if ($percent &gt;= 99)<br />
$class = 1;<br />
else if ($percent &gt;= 70)<br />
$class = 2;<br />
else if ($percent &gt;= 60)<br />
$class = 3;<br />
else if ($percent &gt;= 50)<br />
$class = 4;<br />
else if ($percent &gt;= 40)<br />
$class = 5;<br />
else if ($percent &gt;= 30)<br />
$class = 6;<br />
else if ($percent &gt;= 20)<br />
$class = 7;<br />
else if ($percent &gt;= 10)<br />
$class = 8;<br />
else if ($percent &gt;= 5)<br />
$class = 9;<br />
else<br />
$class = 0;</p>
<p>return $class;<br />
}</p>
<p>/*<br />
* Create the HTML code for each word and apply font size.<br />
*<br />
* @returns string $spans<br />
*/</p>
<p>function showCloud($returnType = "html")<br />
{<br />
$this-&gt;shuffleCloud();<br />
$this-&gt;max = max($this-&gt;wordsArray);</p>
<p>if (is_array($this-&gt;wordsArray))<br />
{<br />
$return = ($returnType == "html" ? "" : ($returnType == "array" ? array() : ""));<br />
foreach ($this-&gt;wordsArray as $word =&gt; $popularity)<br />
{<br />
$sizeRange = $this-&gt;getClassFromPercent(($popularity / $this-&gt;max) * 100);<br />
if ($returnType == "array")<br />
{<br />
$return[$word]['word'] = $word;<br />
$return[$word]['sizeRange'] = $sizeRange;<br />
if ($currentColour)<br />
$return[$word]['randomColour'] = $currentColour;<br />
}<br />
else if ($returnType == "html")<br />
{<br />
$return .= "&lt;span class='word size{$sizeRange}'&gt;   {$word}   &lt;/span&gt;";<br />
}<br />
}<br />
return $return;<br />
}<br />
}<br />
}<br />
?&gt;</p>
<p>&lt;style&gt;<br />
&lt;!--<br />
.word {<br />
font-family: Tahoma;<br />
padding: 4px 4px 4px 4px;<br />
letter-spacing: 3px;<br />
}<br />
span.size1 {<br />
color: #000;<br />
font-size: 2.4em;<br />
}<br />
span.size2 {<br />
color: #333;<br />
font-size:2.2em;<br />
}<br />
span.size3 {<br />
color: #666;<br />
font-size: 2.0em;<br />
}<br />
span.size4 {<br />
color: #999;<br />
font-size: 1.0em;<br />
}<br />
span.size5 {<br />
color: #aaa;<br />
font-size: 1.6em;<br />
}<br />
span.size6 {<br />
color: #bbb;<br />
font-size: 1.4em;<br />
}<br />
span.size7 {<br />
color: #ccc;<br />
font-size: 1.2em;<br />
}<br />
span.size8 {<br />
color: #ddd;<br />
font-size: .8em;<br />
}<br />
span.size0 {<br />
color: #ccc;<br />
font-size: .6em;<br />
}<br />
//--&gt;<br />
&lt;/style&gt;</p>
<p>&lt;?<br />
$randomWords = array(<br />
"webmasterworld", "Computer", "Skateboarding", "PC", "music", "music", "music", "music", "PHP", "C", "XHTML", "eminem", "programming", "forums", "webmasterworld",<br />
"Chill out", "email", "forums", "Computer", "GTA", "css", "mysql", "sql", "css", "mysql", "sql",<br />
"forums", "internet", "class", "object", "method", "music", "music", "music", "music", "gui", "encryption"<br />
);</p>
<p>$cloud = new wordCloud($randomWords);<br />
$cloud-&gt;addWord("music", 12);<br />
$cloud-&gt;addWord("downloads", 8);<br />
$cloud-&gt;addWord("internet", 17);<br />
$cloud-&gt;addWord("PHP", 22);<br />
$cloud-&gt;addWord("CSS", 32);<br />
echo $cloud-&gt;showCloud();<br />
?&gt;</p></blockquote>
<p>And this is how it should look, i have changes some of the colours for the more important words.</p>
<p><span style="font-weight: bold; text-decoration: underline;">How to intergrate with a MySQL database.</span></p>
<p>Depending on how your information is stored in your database, an individual field or a comma seperated field then you can place the tags into the cloud from a MySQL results like follows.</p>
<p>Let's say we have a table called tags, if we have comma seperated values.</p>
<blockquote>
<p class="code">&lt;?php</p>
<p>$cloud = new wordcloud();</p>
<p>$getBooks = mysql_query("SELECT title FROM `tags`");<br />
if ($getBooks)<br />
{<br />
while ($rowBooks = mysql_fetch_assoc($getBooks))<br />
{<br />
$getTags = explode(' ', $rowBooks['title']);<br />
foreach ($getTags as $key =&gt; $value)<br />
{<br />
$value = trim($value);<br />
$cloud-&gt;addWord($value);<br />
}<br />
}<br />
}</p>
<p>$myCloud = $cloud-&gt;showCloud('array');<br />
if (is_array($myCloud))<br />
{<br />
foreach ($myCloud as $key =&gt; $value)<br />
{<br />
echo ' &lt;a href="path/to/tags/'.$value['word'].'" style="font-size: 1.'.$value['sizeRange'].'em"&gt;'.$value['word'].'&lt;/a&gt; &amp;nbsp;';<br />
}</p>
<p>?&gt;</p></blockquote>
<p>If the tag is an individual field, then:</p>
<blockquote>
<p class="code">&lt;?php</p>
<p>$cloud = new wordcloud();</p>
<p>$getBooks = mysql_query("SELECT title FROM `tags`");<br />
if ($getBooks)<br />
{<br />
while ($rowBooks = mysql_fetch_assoc($getBooks))<br />
{<br />
$cloud-&gt;addWord($rowBooks['title']);<br />
}<br />
}<br />
}</p>
<p>$myCloud = $cloud-&gt;showCloud('array');<br />
if (is_array($myCloud))<br />
{<br />
foreach ($myCloud as $key =&gt; $value)<br />
{<br />
echo ' &lt;a href="tags_url/'.urlencode($value['word']).'" style="font-size: 1.'.($value['range']).'em"&gt;'.$value['word'].'&lt;/a&gt; ';<br />
}<br />
?&gt;</p></blockquote>
<p><span style="font-weight: bold; text-decoration: underline;">Video Tutorials</span></p>
<ul>
<li><a href="http://www.youtube.com/watch?v=Woh0e909PRM" target="_blank">Installation Video Tutorial</a></li>
<li><a href="http://www.youtube.com/watch?v=ktLBAxCp3eo" target="_blank">Including Tag Links Video</a></li>
</ul>
<p><span style="text-decoration: underline;"><span style="font-weight: bold;">Finished</span></span><span style="font-weight: bold; text-decoration: underline;">.</span></p>
<p>Ok, so that's the tutorial over. Please have fun with the code, if you are a little confused then please check out the video tutorials, the quality is a little bad at the moment but i hope to arrange some better videos soon!</p>
<p>All comments and questions are welcome, provided they are related to this page.</p>
]]></content:encoded>
			<wfw:commentRss>http://lotsofcode.com/php/tag-cloud-v1.htm/feed</wfw:commentRss>
		<slash:comments>47</slash:comments>
		</item>
		<item>
		<title>Var Vars</title>
		<link>http://lotsofcode.com/php/var-vars.htm</link>
		<comments>http://lotsofcode.com/php/var-vars.htm#comments</comments>
		<pubDate>Wed, 27 Feb 2008 13:22:46 +0000</pubDate>
		<dc:creator>lotsofcode</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://localhost/projects/lotsofcode_2008/snippets/var-vars.htm</guid>
		<description><![CDATA[DemoDownloadVar vars can be very handy, you can use them to associate a name to a variable. &#60;? $my_soon_to_be_variable = 'wow_my_variable'; $$my_soon_to_be_variable = 'Now it's a VAR VAR'; // It's a var var echo $wow_my_variable; // That's right, it's a variable ?&#62;]]></description>
			<content:encoded><![CDATA[<div class="demo-and-download"><a id="dad_demo_anchor" href="/demo/tag-cloud" target="_blank">Demo</a><a id="dad_github_anchor" href="https://github.com/lotsofcode/tag-cloud" target="_blank">Download</a></div><p>Var vars can be very handy, you can use them to associate a name to a variable.</p>
<blockquote>
<p class="code">&lt;?<br />
$my_soon_to_be_variable = 'wow_my_variable';<br />
$$my_soon_to_be_variable = 'Now it's a VAR VAR'; // It's a var var</p>
<p>echo $wow_my_variable; // That's right, it's a variable<br />
?&gt;</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://lotsofcode.com/php/var-vars.htm/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sorting Arrays</title>
		<link>http://lotsofcode.com/php/sorting-arrays.htm</link>
		<comments>http://lotsofcode.com/php/sorting-arrays.htm#comments</comments>
		<pubDate>Wed, 27 Feb 2008 13:22:26 +0000</pubDate>
		<dc:creator>lotsofcode</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[array]]></category>

		<guid isPermaLink="false">http://localhost/projects/lotsofcode_2008/uncategorized/sorting-arrays.htm</guid>
		<description><![CDATA[DemoDownloadYou can sort an array in many different ways using PHP using many different functions, here are a few. sort($array); asort($array); ksort($array); These three different sorting functions all can be sorted in ascending order. The reverse functions are: rsort($array); arsort($array); krsort($array);]]></description>
			<content:encoded><![CDATA[<div class="demo-and-download"><a id="dad_demo_anchor" href="/demo/tag-cloud" target="_blank">Demo</a><a id="dad_github_anchor" href="https://github.com/lotsofcode/tag-cloud" target="_blank">Download</a></div><p>You can sort an array in many different ways using PHP using many different functions, here are a few.</p>
<blockquote>
<p class="code">sort($array);<br />
asort($array);<br />
ksort($array);</p></blockquote>
<p>These three different sorting functions all can be sorted in ascending order.</p>
<p>The reverse functions are:</p>
<blockquote>
<p class="code">rsort($array);<br />
arsort($array);<br />
krsort($array);</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://lotsofcode.com/php/sorting-arrays.htm/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Make all letters caps</title>
		<link>http://lotsofcode.com/php/make-all-letters-caps.htm</link>
		<comments>http://lotsofcode.com/php/make-all-letters-caps.htm#comments</comments>
		<pubDate>Wed, 27 Feb 2008 13:21:38 +0000</pubDate>
		<dc:creator>lotsofcode</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[open source]]></category>

		<guid isPermaLink="false">http://localhost/projects/lotsofcode_2008/snippets/make-all-letters-caps.htm</guid>
		<description><![CDATA[DemoDownloadA nice little way of making all letters into capitals is to use PHP's inbuilt function strtoupper, like so. echo strtoupper('Hi, i am going to be all in caps in just one mo'); Will print ... HI, I AM GOING TO BE ALL IN CAPS IN JUST ONE MO]]></description>
			<content:encoded><![CDATA[<div class="demo-and-download"><a id="dad_demo_anchor" href="/demo/tag-cloud" target="_blank">Demo</a><a id="dad_github_anchor" href="https://github.com/lotsofcode/tag-cloud" target="_blank">Download</a></div><p>A nice little way of making all letters into capitals is to use PHP's inbuilt function strtoupper, like so.</p>
<blockquote>
<p class="code">echo strtoupper('Hi, i am going to be all in caps in just one mo');</p>
</blockquote>
<p>Will print ...</p>
<blockquote>
<p class="code">HI, I AM GOING TO BE ALL IN CAPS IN JUST ONE MO</p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://lotsofcode.com/php/make-all-letters-caps.htm/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Validation</title>
		<link>http://lotsofcode.com/php/validation.htm</link>
		<comments>http://lotsofcode.com/php/validation.htm#comments</comments>
		<pubDate>Wed, 27 Feb 2008 13:17:00 +0000</pubDate>
		<dc:creator>lotsofcode</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://localhost/projects/lotsofcode_2008/uncategorized/validation.htm</guid>
		<description><![CDATA[DemoDownloadn this day and age and taking into consideration the evolution of the web, allot of things are overlooked when programming and one of the main subjects I see commonly being 223overlooked" is validation. Validation isn't that difficult you just need to question the integrity of every string trying to be parsed, the most common [...]]]></description>
			<content:encoded><![CDATA[<div class="demo-and-download"><a id="dad_demo_anchor" href="/demo/tag-cloud" target="_blank">Demo</a><a id="dad_github_anchor" href="https://github.com/lotsofcode/tag-cloud" target="_blank">Download</a></div><p>n this day and age and taking into consideration the evolution of the web, allot of things are overlooked when programming and one of the main subjects I see commonly being 223overlooked" is validation.</p>
<p>Validation isn't that difficult you just need to question the integrity of every string trying to be parsed, the most common use of validation is along side forms and that's what I am going to be discussing today.</p>
<p>Let's start with a simple form with a name, age, e-mail and comments form objects.</p>
<blockquote>
<p class="code">&lt;form method="post"&gt;<br />
&lt;input type="text" name="name"&gt;<br />
&lt;input type="text" name="age"&gt;<br />
&lt;input type="text" name="email"&gt;<br />
&lt;textarea name="comments"&gt; &lt;/textarea&gt;<br />
&lt;input type="submit" name="submit"&gt;<br />
&lt;/form&gt;</p></blockquote>
<p>Now, usually when a form is posted all the values are parsed and if we are using an e-mail script to send an e-mail then the e-mail script is venerable of being attacked using a technique called 223e-mail injection", this means that people can parse more information then you want them to.</p>
<p>Let's start with the name field, it is a required field so we need to validate that the name field has a value, to do this I am going to test the integrity of the value parsed using the function empty (http://uk3.php.net/empty). This is how I would see if the field name is 223empty".</p>
<blockquote>
<p class="code">&lt;?<br />
If (empty($_POST['name']))<br />
{<br />
$errors[] = 'Please enter a name';<br />
}<br />
?&gt;</p></blockquote>
<p>As you can see from the above example I initiated an array called errors and added the value please enter a name, this array will be used later.</p>
<p>The next field is the age field, now because the value of the field should be a numeric value we will also check to see if the value parsed is numeric using the is_numeric (http://uk3.php.net/is_numeric) function like so.</p>
<blockquote>
<p class="code">&lt;?<br />
if (empty($_POST['age']))<br />
{<br />
$errors[] = 'Please enter a age';<br />
}<br />
else if (!is_numeric($_POST['age']))<br />
{<br />
$errors[] = 'Please enter a valid age with a numeric value';<br />
}<br />
?&gt;</p></blockquote>
<p>Next we need to validate the e-mail address, I have seen this done many ways but the best way in my opinion is with a regular expression, so something like this should be sufficient enough to stop people trying to parse multiple e-mail addresses.</p>
<blockquote>
<p class="code">&lt;?<br />
if (empty($_POST['email']))<br />
{<br />
$errors[] = 'Please enter an e-mail';<br />
}<br />
else if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $_POST['email']))<br />
{<br />
$errors[] = 'Please enter a valid e-mail address';<br />
}<br />
?&gt;</p></blockquote>
<p>Finally comments, identical to the name field although because the comments field is a textarea we do not have any control over the length of the value, so if you think it's necessary you can add a length check like this.</p>
<blockquote>
<p class="code">&lt;?<br />
if (empty($_POST['comments']))<br />
{<br />
$errors[] = 'Please enter some comments';<br />
}<br />
else if (strlen ($_POST['comments']) &gt; 255)<br />
{<br />
$errors[] = 'Your comment is too long, please do not submit more then 255 characters';<br />
}<br />
?&gt;</p></blockquote>
<p class="MsoNormal">Then once all the validation fields have been assigned you can utilize the error messages (if they exist) like so.<o:p></o:p></p>
<blockquote>
<p class="code">if (count($errors) == 0)<br />
{<br />
// Process form<br />
}<br />
else<br />
{<br />
echo $errors[0];<br />
}</p></blockquote>
<p class="MsoNormal">That's basically the round trip of validation, these are very important aspects of maintaining secure forms, just to make things easier here is the code in full and i have added a html table with labels for each field.</p>
<blockquote>
<p class="code">&lt;?<br />
if (empty($_POST['name']))<br />
{<br />
$errors[] = 'Please enter a name';<br />
}</p>
<p>if (empty($_POST['age']))<br />
{<br />
$errors[] = 'Please enter a age';<br />
}<br />
else if (!is_numeric($_POST['age']))<br />
{<br />
$errors[] = 'Please enter a valid age with a numeric value';<br />
}</p>
<p>if (empty($_POST['email']))<br />
{<br />
$errors[] = 'Please enter an e-mail';<br />
}<br />
else if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $_POST['email']))<br />
{<br />
$errors[] = 'Please enter a valid e-mail address';<br />
}</p>
<p>if (empty($_POST['comments']))<br />
{<br />
$errors[] = 'Please enter some comments';<br />
}<br />
else if (strlen ($_POST['comments']) &gt; 255)<br />
{<br />
$errors[] = 'Your comment is too long, please do not submit more then 255 characters';<br />
}</p>
<p>if (count($errors) == 0)<br />
{<br />
// Process form<br />
}<br />
else<br />
{<br />
echo $errors[0];<br />
}<br />
?&gt;</p>
<p>&lt;form method="post"&gt;<br />
&lt;table&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;Name:&lt;/td&gt;<br />
&lt;td&gt;&lt;input type="text" name="name"&gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;Age:&lt;/td&gt;<br />
&lt;td&gt;&lt;input type="text" name="age"&gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;E-mail:&lt;/td&gt;<br />
&lt;td&gt;&lt;input type="text" name="email"&gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;Comments:&lt;/td&gt;<br />
&lt;td&gt;&lt;input name="comments" &gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td colspan="2"&gt;&lt;textarea name="comments"&gt; &lt;/textarea&gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;/table&gt;<br />
&lt;/form&gt;</p></blockquote>
<p class="MsoNormal">if you do have any other questions then please leave a comment below.</p>
]]></content:encoded>
			<wfw:commentRss>http://lotsofcode.com/php/validation.htm/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Files Uploading, Validation</title>
		<link>http://lotsofcode.com/php/uploading-files.htm</link>
		<comments>http://lotsofcode.com/php/uploading-files.htm#comments</comments>
		<pubDate>Wed, 27 Feb 2008 13:15:23 +0000</pubDate>
		<dc:creator>lotsofcode</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[upload]]></category>

		<guid isPermaLink="false">http://localhost/projects/lotsofcode_2008/uncategorized/uploading-files.htm</guid>
		<description><![CDATA[DemoDownloadUploading file is one of the most convenient things to do with PHP. This tutorial will cover how to upload files safely and how to ensure no files are replaced in the process. The first step would be creating your form, this will be a basic HTML form with the attribute encrypt added to it, [...]]]></description>
			<content:encoded><![CDATA[<div class="demo-and-download"><a id="dad_demo_anchor" href="/demo/tag-cloud" target="_blank">Demo</a><a id="dad_github_anchor" href="https://github.com/lotsofcode/tag-cloud" target="_blank">Download</a></div><p><span>Uploading file is one of the most convenient things to do with PHP. This tutorial will cover how to upload files safely and how to ensure no files are replaced in the process.</span></p>
<p>The first step would be creating your form, this will be a basic HTML form with the attribute encrypt added to it, this tells PHP we are trying to upload a file, you will not be able to upload a file without it. I have also added a button to the forum, this will be the event handling button, we will check to see if this button has been posted before we try and upload a file.</p>
<blockquote>
<p class="code">  &lt;form action="" method="post"  enctype="multipart/form-data"&gt;<br />
&lt;input type="file" name="uploaded_file"&gt;<br />
&lt;input type="submit" name="sendForm" value=" upload image"&gt;<br />
&lt;/form&gt;</p></blockquote>
<p>Next we check to see if the button has been submitted, we identify this by checking for the posted super global named sendForm as this is the name of the button on our form.</p>
<blockquote>
<p class="code"> if (!empty($_REQUEST['sendForm']))<br />
{<br />
// Upload the files process will go here.<br />
}</p></blockquote>
<p>When a file type is attached to a form and the form is posted the file is uploaded to a tempory directory called /tmp usually, once the file has uploaded you need to move the file from the temp directory into your preferred directory.</p>
<p>We can access the files array by using the files array super global.</p>
<p>The following is posted in the $_FILES array.</p>
<p>[tmp_name] the location of the temporary file and the name of the file<br />
[name] the original file name<br />
[errors] error number, this will be zero if everything is ok<br />
[size] the size of the file.</p>
<p>This is how we access the files array, it is a multi-dimensional array so we need the name of the posted file and then the attribute in each call to the super global.</p>
<blockquote>
<p class="code"> if (!empty($_REQUEST['sendForm']))<br />
{<br />
$name = $_FILES['uploaded_file']['name']; // Assign the name to a variable<br />
$tmp_name = $_FILES['uploaded_file']['tmp_name']; // Assign the tmp_name to a variable<br />
$error = $_FILES['uploaded_file']['error']; // Assign the error to a variable<br />
$size = $_FILES['uploaded_file']['size']; // Assign the size to a variable<br />
}</p></blockquote>
<p>You should also declare the directory where you want to save the file, make sure the directory permissions allows you to upload to this folder, this can be done by using a client like &lt;a href="http://www.pspad.com"&gt;PsPad&lt;/a&gt; or &lt;a href="filezilla.sourceforge.net/"&gt;FileZilla&lt;/a&gt; by clicking the folder and changing the permissions (CHMOD).</p>
<blockquote>
<p class="code"> $uploadFilesTo = 'my_folder/my_file'; // No trailing slash</p>
</blockquote>
<p>Now we have assigned the names of all the files array then can be called directly from the variables.</p>
<p>Now because we have no control over the name of the file i suggest we rename the file by removing any non-standard characters, this can be done like so.</p>
<blockquote>
<p class="code"> $name = ereg_replace('[^A-Za-z0-9.]', '-', $name);</p>
</blockquote>
<p>This will basically remove anything apart from characters, numbers and the full stop for the file extension.</p>
<p>Now we have a safe filename let's see see if it's one of the file types we don't allow people to upload, so we are going to create a comparison array for this.</p>
<blockquote>
<p class="code"> $naughtyFileExtension = array("php", "php3", "asp", "inc", "psd");</p>
</blockquote>
<p>Now we have the array we need to compare the file extension against the array, like so. This also requires us to get the file extension using the &lt;a href="http://uk3.php.net/pathinfo/"&gt;pathinfo&lt;/a&gt; function.</p>
<blockquote>
<p class="code"> $fileInfo = pathinfo($name); // Returns an array that includes the extension</p>
<p>if (!in_array($fileInfo['extension'], $naughtyFileExtension))<br />
{<br />
// The file is ok, we can upload it.<br />
}<br />
else<br />
{<br />
// The file uses an extension we don't want to upload<br />
}</p></blockquote>
<p>Now, if we have gotten this far and we want to upload the file then the next step is to ensure that the file doesn't already exist on the server because by default it will replace the old file, this can be done using the function &lt;a href="http://uk3.php.net/file_exists"&gt;file_exists, but i am also going to use a recursive function to continuously keep checking until it doesn't exist&lt;/a&gt;.</p>
<blockquote>
<p class="code"> $name = getNonExistingFilename($uploadFilesTo, $name);</p>
<p>function getNonExistingFilename($uploadFilesTo, $name)<br />
{<br />
if (!file_exists($uploadFilesTo.'/'.$name))<br />
return $name;</p>
<p>return getNonExistingFilename($uploadFilesTo, rand(100, 200).'_'.$name);<br />
}</p></blockquote>
<p>This will add a prefix onto the image and will ensure it won't be replaced. If you prefer you could add a suffix before the extension.</p>
<p>Now if we can clarify the file doesn't exist then we can upload the file like so.</p>
<blockquote>
<p class="code"> if (move_uploaded_file ($tmp_name, $name))<br />
{<br />
echo '&lt;p&gt;File uploaded&lt;/p&gt;';<br />
}<br />
else<br />
{<br />
echo '&lt;p&gt;File failed to upload&lt;/p&gt;';<br />
}</p></blockquote>
<p>So that's it, this is pretty durable code, have fun with it.</p>
<blockquote>
<p class="code"> &lt;?php</p>
<p>error_reporting(E_ALL ^ E_NOTICE); // Show all major errors.</p>
<p>// Check to see if the button has been pressed<br />
if (!empty($_REQUEST['sendForm']))<br />
{<br />
// Assign the name to a variable<br />
$name = $_FILES['uploaded_file']['name'];<br />
// Assign the tmp_name to a variable<br />
$tmp_name = $_FILES['uploaded_file']['tmp_name'];<br />
// Assign the error to a variable<br />
$error = $_FILES['uploaded_file']['error'];<br />
// Assign the size to a variable<br />
$size = $_FILES['uploaded_file']['size'];<br />
// No trailing slash<br />
$uploadFilesTo = 'up';<br />
// Create safe filename<br />
$name = ereg_replace('[^A-Za-z0-9.]', '-', $name);<br />
// Disallowed file extensions<br />
$naughtyFileExtension = array("php", "php3", "asp", "inc", "psd");<br />
// Returns an array that includes the extension<br />
$fileInfo = pathinfo($name);<br />
// Check extension<br />
if (!in_array($fileInfo['extension'], $naughtyFileExtension))<br />
{<br />
// Get filename<br />
$name = getNonExistingFilename($uploadFilesTo, $name);<br />
// Upload the file<br />
if (move_uploaded_file($tmp_name, $uploadFilesTo.'/'.$name))<br />
{<br />
// Show success message<br />
echo '&lt;p&gt;File uploaded to '.$uploadFilesTo.'/'.$name.'&lt;/p&gt;';<br />
}<br />
else<br />
{<br />
// Show failure message<br />
echo '&lt;p&gt;File failed to upload to '.$uploadFilesTo.'/'.$name.'&lt;/p&gt;';<br />
}<br />
}<br />
else<br />
{<br />
// Bad File type<br />
echo '&lt;p&gt;The file uses an extension we don't want to upload&lt;/p&gt;';<br />
}<br />
}</p>
<p>// Functions do not need to be inline with the rest of the code<br />
function getNonExistingFilename($uploadFilesTo, $name)<br />
{<br />
if (!file_exists($uploadFilesTo . '/' . $name))<br />
return $name;</p>
<p>return getNonExistingFilename($uploadFilesTo, rand(100, 200) . '_' . $name);<br />
}<br />
?&gt;</p>
<p>&lt;form action="" method="post"    enctype="multipart/form-data"&gt;<br />
&lt;input type="file" name="uploaded_file"&gt;<br />
&lt;input type="submit" name="sendForm" value=" upload image "&gt;<br />
&lt;/form&gt;</p></blockquote>
<p>Good luck.</p>
]]></content:encoded>
			<wfw:commentRss>http://lotsofcode.com/php/uploading-files.htm/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Sticky Caps</title>
		<link>http://lotsofcode.com/php/sticky-caps.htm</link>
		<comments>http://lotsofcode.com/php/sticky-caps.htm#comments</comments>
		<pubDate>Wed, 27 Feb 2008 13:10:16 +0000</pubDate>
		<dc:creator>lotsofcode</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[upload]]></category>

		<guid isPermaLink="false">http://localhost/projects/lotsofcode_2008/uncategorized/sticky-caps.htm</guid>
		<description><![CDATA[DemoDownloadEver bothered thinking about how to make your site stand out a little more? I was think about this a while back and what came to mind was sTicKy cApS, i think that's the terminology, well anyway here is the code to create "sticky caps". First of all we need a string to apply this [...]]]></description>
			<content:encoded><![CDATA[<div class="demo-and-download"><a id="dad_demo_anchor" href="/demo/tag-cloud" target="_blank">Demo</a><a id="dad_github_anchor" href="https://github.com/lotsofcode/tag-cloud" target="_blank">Download</a></div><p><span>Ever bothered thinking about how to make your site stand out a little more?</span></p>
<p>I was think about this a while back and what came to mind was sTicKy cApS,<br />
i think that's the terminology, well anyway here is the code to create "sticky caps".</p>
<p>First of all we need a string to apply this function to.</p>
<p>So let's create a string.</p>
<blockquote>
<p class="code">&lt;?<br />
// This is your string, write whatever you want!<br />
$string = 'created by derekharvey is this cool?';<br />
?&gt;</p></blockquote>
<p>Now we have our string let's move onto the function that creates the sticky caps.</p>
<blockquote>
<p class="code">&lt;?<br />
function sticky_caps($over)<br />
{<br />
// php placeholder<br />
}<br />
?&gt;</p></blockquote>
<p>So now we have the placeholder for our function and the first argument of our function we can start programming.</p>
<blockquote>
<p class="code">&lt;?<br />
$return = "";<br />
?&gt;</p></blockquote>
<p>So, first we declare the variable $return so this string can be concatenated and not display an error for some server settings.</p>
<p>Next, we break the string ($over) into an array or by using the function explode and using the space as the delimiter.</p>
<blockquote>
<p class="code">&lt;?<br />
$over = explode(' ', $over);<br />
?&gt;</p></blockquote>
<p>Then we loop through each of the words and then loop through all the letters in the word and we use modulo to determine of the number is odd or even and if it's odd we capitalize the work and if not then we set it to lower case.</p>
<p>We assign all the words to the same array and then after each work we add a space back into the loop.</p>
<blockquote>
<p class="code">&lt;?<br />
foreach($over as $overs)<br />
{<br />
for ($i = 0; $i &lt; strlen($overs); $i++)<br />
{<br />
if ($i % 2 == 0)<br />
{<br />
// assign the lowercase letters back into the array<br />
$bits[] = strtolower($overs[$i]);<br />
}<br />
else<br />
{<br />
// assign the uppercase letters back into the array<br />
$bits[] = strtoupper($overs[$i]);<br />
}<br />
}<br />
// add the space beetween the words<br />
$bits[] = ' ';<br />
}<br />
?&gt;</p></blockquote>
<p>Next we put all the array items back into the string.</p>
<blockquote>
<p class="code">&lt;?<br />
$overs = implode('', $bits);<br />
?&gt;</p></blockquote>
<p>and then we return the value</p>
<blockquote>
<p class="code">&lt;?<br />
return $overs;<br />
?&gt;</p></blockquote>
<p>Then to test the script we do something like this.</p>
<blockquote>
<p class="code">&lt;?<br />
echo sticky_caps($string);<br />
?&gt;</p></blockquote>
<p>You can also put the string directly into the function.</p>
<blockquote>
<p class="code">&lt;?<br />
echo sticky_caps("Derek Harvey Website Developer and Programmer");<br />
?&gt;</p></blockquote>
<p>This should present you with something like so:</p>
<blockquote><p>dErEk hArVeY wEbSiTe dEvElOpEr aNd pRoGrAmMeR</p></blockquote>
<p>--</p>
<p>Just to make it a little easier for you, here it the complete code:</p>
<p class="code" align="left">&lt;?<br />
$string = 'derek harvey website developer and programmer';</p>
<p align="left">&nbsp;</p>
<p align="left">function sticky_caps($over)<br />
{<br />
$return = "";</p>
<p align="left">&nbsp;</p>
<p align="left">$over = explode(' ', $over);</p>
<p align="left">&nbsp;</p>
<p align="left">foreach ($over as $overs)<br />
{<br />
for ($i = 0; $i &lt; strlen($overs); $i++)<br />
{<br />
if ($i % 2 == 0)<br />
{<br />
// assign the lowercase letters back into the array<br />
$bits[] = strtolower($overs[$i]);<br />
}<br />
else<br />
{<br />
// assign the uppercase letters back into the array<br />
$bits[] = strtoupper($overs[$i]);<br />
}</p>
<p align="left">&nbsp;</p>
<p align="left">}<br />
// add the space beetween the words back as we removed it when we exploded the array<br />
$bits[] = ' ';<br />
}</p>
<p align="left">&nbsp;</p>
<p align="left">// put the peices back together<br />
$overs = implode('', $bits);</p>
<p align="left">&nbsp;</p>
<p align="left">// return the overal value<br />
return $overs;<br />
}</p>
<p align="left">&nbsp;</p>
<p align="left">echo sticky_caps($string);<br />
?&gt;</p>
<p>Thanks for listening.</p>
]]></content:encoded>
			<wfw:commentRss>http://lotsofcode.com/php/sticky-caps.htm/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

