20CodeIgniter uploads, allow any filetype

CodeIgniter uploads, allow any filetype

CodeIgniter, the brilliant PHP framework currently does not allow you to just upload any filetype using the built in upload library. Although it’s generally a bad idea to allow your users to upload any types of file there may be the occassional need to do exactly this.

The way CodeIgniter currently does it is by making you list all of the filetypes which you’d like to let the user upload. Unless you want to sit there for a while typing a very long list why not do this simple trick. This is only a temporary fix and isn’t ideal as it does involve editing the CodeIgniter libraries but it does work. This will allow you to upload any type of file using the upload library.

Simply open up ‘system/libraries/Upload.php’ and do a search for ‘function is_allowed_filetype’. That should take you to the function used to determine whether or not the file upload should be accepted.

Now all you have to do is put ‘return true’ inside that function, right at the top. You should now be able to upload literally any type of file. Be careful though! As I said just a second ago this is generally a bad idea and can open up some very serious security holes!

3Redirect sub domain to another host

Redirect sub domain to another host

I manage several hosting accounts all of which have a specific purpose. For example, I have an account for hosting my own personal sites, another for client sites and then a third host for development purposes. With my development server this is where I’ll develop various sites and applications which I can then demo to the client. However, to maintain a consistent brand whilst keeping the development stuff away from the live stuff means that I’ve had to redirect a sub domain from my live server to the development server. I do this for each new client I do work for.

So how exactly do you do this? Well, let’s say I wanted to redirect the sub-domain “something.michaelgarethmorgan.com” to another hosting account which is on a completely different server. I’d need to know the IP address of the alternate host as well as have cPanel access on both servers.

So, on the main server in cPanel go to “Simple DNS Zone Editor” which is a utility that lets you create A and CNAME records. For this task we’re going to create a new A record. In the name field simply type “something” – whatever you put here will be used as the sub-domain. In the address box type in the IP address of the alternate host, then click “Add A Record”.

Now, log into cPanel on the alternative server. Here, we’re going to simply add a new sub-domain, in this case it would be “something”. Once the sub-domain is created whenever I were to visit “something.michaelgarethmorgan.com” it would serve the files from the alternate server without you even knowing it.

This can be very useful if you’d like to keep your media and bulky files stored on a separate server away from the one handling your everyday web traffic. This can not only reduce server load but can also make it easier to maintain your files.

And that’s it. Redirecting a sub-domain to another host only takes a minute or two.

22SEO – The domain is vital

SEO – The domain is vital

Importance of domain nameOver the past few years and in particularly the last 18 months I’ve come to realize what I consider to be the most important factor for getting ranked high in the search engine results of todays search engines. It’s all about the domain name.

Okay, so maybe it’s not ALL about the domain name but the domain definitely plays a very big part. I’m saying this not based on scientific facts or even based on any particular tests or experiments but instead I based this on my own experience with SEO. Over the past 18 months or so I’ve launched several sites, some of which have achieved a page #1 ranking in Google whilst others have not. For each of these sites I’ve done the standard search engine optimization tasks including optimizing keywords, link building, article marketing and more. Looking over my sites I can see a trend that in general the sites whose domains are either an exact or very similar match to their primary keyword/phrase seem to be ranking much better compares to those sites whose domains are slightly less specific.

From this I’m led to believe that one of the most important things you can do if you intend to rank well in the search engines would be to make sure you choose the right domain name. I am by no means saying that choosing the perfect domain on its own will get you ranked higher. Instead I’m simply saying that a better, more specifically matched domain from what I can see has a big impact on how your site will rank (at least with Google).

5Themedy – Free WordPress theme

Themedy – Free WordPress theme

I’d like to share with you my latest free WordPress theme which is called Themedy. This free WordPress theme comes packed with plenty of cool features including four colour schemes, a customizable image scroller and much more. You can quickly and easily change things like the colour scheme, the scrolling images (and their link), footer text, navigation text and more right from the administration panel.

Free WordPress theme - Themedy

A demo is available here.
This WordPress is available for use for free and can be downloaded here.

24jQuery toggle fade in/out

jQuery toggle fade in/out

jQuery toggle fade in/outFor a recent interface design I had to apply a little jQuery to make some of the page interactions more intuitive (and nicer to use). One of the things needed was a quick and easy to way to toggle a particular elements display but in a way so that it faded in and out. The idea was simple – have a class which could be attached to a bunch of anchor tags which when clicked would fade in a div element. Then, upon clicking on that link (or any of the other links which also had the same class) the div element would fade out.

It’s actually quite easy really. Here’s the code for toggling an element on click with jQuery.

1
$('#hiddenbox').animate({opacity: 'toggle'}, 'fast');

To use it simply have an anchor tag like this…

1
<a href="" class="toggle-fade">Toggle box</a>

And then the jQuery code…

1
2
3
4
5
6
$(document).ready(function(){
	$('.toggle-fade').click(function(){
		$('#hiddenbox).animate({opacity: 'toggle'}, 'fast');
		return false;
	});
});

Perfect!

23CodeIgniter model database interaction

CodeIgniter model database interaction

Recently I’ve been doing quite a bit of work with the PHP MVC framework, CodeIgniter. First of all – it’s great! I’ve always been a fan of object oriented programming software approaches and the CodeIgniter handles this almost perfectly. As my CodeIgniter based projects start to grow, more and more database tables are introduced which is made a lot easier with the model part of the MVC design pattern.

When it comes to CodeIgniter though I’ve found that all of my table models all have the same 2 common functions. To me it makes sense to have this functionality built into the parent ‘Model’ class within CodeIgniter but since they’re not here they are for you to add them yourself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function get($where = array(), $limit = 1000, $offset = 0)
{
	$rows = array();
 
	$this->db->select('*');
	$this->db->where($where); 
	$query = $this->db->get("tablename", $limit, $offset);
 
	// How many rows?
	if($query->num_rows() > 0)
	{
		// Loop through each row
		foreach($query->result() AS $row)
		{
			$rows[] = $row;
		}
	}
 
	return $rows;
}
 
function get_by_id($ID, $where = array())
{
	$where = array_merge(array('ID' => $ID), $where);
	$rows = $this->get($where, 1);
	return $rows[0];
}

So what exactly are these two functions? They’re a quick and simple way for retrieving information from a database table using the relevant model within your application. By simply calling either of these functions you can quickly retrieve whatever data required and then carry on without needing to worry about table names, etc. So, if for example for whatever reason you had to rename a table you’d only need to modify it in just the one place (in the model) as opposed to having to wade through all your code if you’ve typed it manually. Sure, the likelyhood of you changing a table name is quite slim but you never know.

Of course you may want to extend this to allow for updates, deletes and inserts which only takes a few minutes. Just thought I’d put this out there in case anyone’s interested.

3Google PageRank update

Google PageRank update

GoogleAt last, the big G finally rolled out a PageRank update. I can’t even remember the last time I saw an update happen so I was happy to see this one. All of my new sites (since the update) have been given some PR juice, all pretty low though – highest is PR3, some at PR2 and most at PR1. Some of my older sites lost their PR; they were only at PR1 though so that’s no problem really.

I just hope these PageRank updates become a bit more frequent. It’s nice to have some sort of realizable ladder to climb when doing SEO.

23WordPress post scheduling

WordPress post scheduling

WordPress Post SchedulingI just wanted to let you all know about post scheduling in WordPress. If you haven’t already given it a go I’d highly recommend you go take a look. Basically it allows you to instead of publishing a post immediately you can schedule it to be published at a specific time and date in the future. This means that if one day you manage to crunch out more posts than you normally would you could have them published in a few days or so instead.

For me this is great as I maintain quite a large amount of desktop wallpaper sites, all running on WordPress. All of these sites need to be kept up to date with regular wallpaper updates. Instead of dropping by every other day and adding a new wallpaper I just add a load of them every two weeks. Then, over the course of the next two weeks the wallpaper posts are automatically published without me doing anything.

Another great use for this is if you plan on going away for a little while. For example, over Christmas I’ll be away for about 3 weeks (Spain, wooohoooo!) but you can be sure that my sites will be kept up to date.

So go ahead, check it out! It’s on the top right hand side of the “Add New” section of WordPress.

22Using numbers to represent data

Using numbers to represent data

I’m currently in the process of rebuilding one of my older sites from the ground up. One thing that the new version makes use of is a field in several database tables called ‘state’. Basically, this holds an integer value ranging from 0 upwards where each number represents a specific meaning. For example…

0: Inactive
1: Active
2: Processed
3: Archived

The reasons for doing this are quite simple; it saves space in the database and is also a lot easier (and nicer) to work with.

Now, previously, when for example I wanted to loop through each row and display the textual meaning of each of the values I would either use a switch statement or a big if-else statement block. Now though I make use of a much simpler method…

1
2
3
4
5
6
7
8
9
function number_state($state = 0, $labels = array())
{
	$text = $labels[$state];
 
	if($text == "")
		$text = "Unknown";
 
	return $text;
}

This function simply takes two parameters. The first is the number itself; the second is an array of the corresponding value meanings. To use it just do this…

1
echo number_state(1, array("State zero", "State one", "State two", "State three"));

Or, in a simple loop…

1
2
3
4
for($i = 0; $i <= 3; $i++)
{
	echo number_state($i, array("State zero", "State one", "State two", "State three"));
}

It’s a nice and simple solution to a simple problem. From my perspective it is much better having something like this compared to big chunks of code doing the same thing.

11How to use bookmarklets

How to use bookmarklets

BookmarkletI’ve just added a whole bunch of tweaks to my URL shortening site, Shrten. Some of the changes are visual though some are under the hood making the site easier to use and overall much, much better. But this post isn’t about that; it is instead about bookmarklets and how you can use them on your own site.

On Shrten you can now drag a link into your browsers toolbar and then when clicked it will instantly create you a shorter url for the page you are currently viewing.

This is the first time I’ve ever used a bookmarklet on any of my sites and I have to say that for apps and services such as URL shorteners it works fantastically! So, how can you do it?

It’s quite simple to be honest and consists of just a single link with a tiny bit of JavaScript. Take a look at this:

1
<a href="javascript:void(location.href='http://shrten.com/url/'+location.href)">Shrten</a>

The little snippet above is the exact bit of code I’m using on my site. Basically, when clicked it will redirect the user to http://shrten.com/x where x is the url of the current page the user is viewing. This can be put to all sorts of uses and I definitely recommend that you should take a look at it. It’s easy to implement and even easier to use!

Page 1 of 212