Lessons I’ve learned working with Flash File Uploaders

For the last two weeks I’ve worked with two Flash File Uploaders:

I this post I’ll describe the problems I had with it and how I solved them.

Lesson 1: NO COOKIES

If you add the uploader to authentication based application you might see no files actually got uploaded.

Why is that? your/clients cookies are not recognizable with the Flash Uploader, so the uploader is like every other intruder that need to perform a login.
How I found out? Luckily I used Fiddler2 [more about it later] and the error/debug messages logging system I had helped me to find out what went wrong.
Solution? All modern uploaders support custom post data, so you can use it for session tokens or PHPSSID.

Lesson 2: Upload URL isn’t Found?

On my old development server I had the strangest problem ever – the existing-not-authenticated url that uploads the files was not found by the script.
I’ve activated Fiddler2 and saw that the response headers were 302 – not found. My logging messages showed me that no one accessed the url.

Why is that? In my case the problem was in my old development server provider – they have some mechanism that filters requests for some reason.
How I found out? This is the place to thank Fiddler2 – Fiddler2 is a HTTP monitoring software and FireFox addon that.. monitors HTTP requests. With Fiddler2 I saw the response headers and after few tryouts I’ve got as a response an abuse page of my server provider.
Solution? I contacted my server provider and they did what they had to in order for my uploader to work.

Lesson 3: Don’t be rude to Mac

Meanwhile, I switched the flash uploader and start using SWFUpload which is much much better in my opinion.
My upload script contains photo resizing and uploading to a distant server. Every special event is logged, every suspicious action is logged.
The nature of those flash uploaders is uploading the file, and then they reach 100% – but no Complete event because my script running now the resizing stuff.When the script finished, the uploader moved to the next file in queue, but for Mac users, even after the resizing, the file never got the “Complete” event.

Why is that? As I said, every little even in the upload process is logged, if something bad happens I know how to handle it. I didn’t echo/print any response, because I don’t need to, but Mac have to get some kind of response in order to move on. Its not clear to me why, but that’s the way it works.
How I Found Out?
really, just searched the web.
Solution?
just echo something..it might be useful if you wish to return event to the uploader and let him handle it.

Posted in CodeIgniter, PHP, Web Development at February 8th, 2010. No Comments.

What are you interested in?

Hi,
For the last few weeks I’ve noticed [with help of Google Analytics] that most of my traffic is for CodeIgniter information. For example, yesterday someone spent 30 minutes reading several posts if mine.

I want you to tell me, what information you’re looking for, what questions you have, I’ll be glad to write about it.

Posted in CodeIgniter, Web Development at February 7th, 2010. 2 Comments.

Developing Content Managment System with CodeIgniter – Part 3

Hi.
Well, As my development process continues I would like to write you about the features and structure of my application now.

I’ve talked about the basics in my previous posts, I covered the structure of my application [modules], I showed you how I extend CI_Controller and CI_Model. Now I want to share with you the which features I have in my application that makes my live easier AND how I extended Controller and Model even more.

Features

    1. I Followed Phil Sturgeon’s How-To article for Support multiple production environments in CodeIgniter. This way I defined what to log and where, in what level, and the url’s of the local server, development server and production server [and Databases too].
    2. I’ve extended CI_Log class so its sending me email if some Error happens in PRODUCTION server [this is the application that the client is using].
    This way I can easily know what went wrong when I get the phone call from my client.
    3. History Class -> I wrote you in my Must have features in your CMS post about System Restore. So I wrote a simple model that has this method in it:
	/**
	 * This method writes data to history.
	 * @param String $table
	 * @param Integer $row
	 * @param String $field
	 * @param String $oldValue
	 * @param String $newValue
	 */
	public function write($table,$row, $field, $newValue) {

		// select only the field I need
		$this->db->select($field);
		// get the right row
		$data = $this->db->get_where($table, array('ID' => $row));
		// take the row and break to array
		$currentState = $data->row_array();
		// store the old value
		$oldValue = $currentState[$field];

		if($oldValue != $newValue) {
			$history = array(
				'TableName' => $table,
				'RowIndex' => $row,
				'FieldName' => $field,
				'OldValue' => $oldValue,
				'NewValue' => $newValue,
				'Created' => time()
			);
			$this->db->insert('history', $history);
			return TRUE;
		} else {
			return FALSE;
		}
	}

*I already see a mistake in this method – I have $this->table property in each model, and I don’t use it here – I write the table name directly in the insert method.*
This method activates before each update() method in my Models. The update() method written once in my MY_Model class, so I don’t really writing data to this “system restore” table myself [by calling this method each time] – everything happens in the background.

Extended CI_Controller

Well, I can’t share with you the whole code [I'm not allowed, I'll share certain methods], But I can write you the properties and methods I have and the purpose of them.

	// Module name. gallery/cart/content/contact/users...
	protected $module;

	// Basic authentication and permission system, UserLevel gets the
	// minimum level needed to enter this controller
	protected $userLevel;

	// The name of the controller
	protected $controllerName;

	public function MY_Controller() {
		parent::Controller();
		if(ENV == 'dev' OR ENV == 'local') $this->output->enable_profiler(TRUE);
		else if(ENV == 'live') $this->output->enable_profiler(FALSE);
		$this->controllerName = get_class($this);
	}

As you can see, If its a development environment I’m enabling the profiler, I use controllerName for better logging [knowing where error happened or any other message level].

I also have 2 methods that are loading views and models:

	/**
	 * This method is a shortcut for loading a view in my controllers.
	 * @param String $name The name of the view I want to load, inside the module folder.
	 * @param Text $content The HTML content I want the view to show.
	 * @param Bool $return Return the view content, or just show it?
	 */
	protected function _view($name, $content, $return = FALSE) {
		return $this->load->view($this->module.'/'.$name, $content, $return);
	}

The reason I did it is that I got tired writing

$this->load->view($this->module.'/viewName', '', TRUE)

And the current method really saves some time.

Extended CI_Model

	// The main table name
	public $table;
	private $modelName;

	public function MY_Model() {
		parent::Model();
		$this->table = '';
		$this->modelName = get_class($this);
	}

	// Log as Error each time none-existing method called.
	public function __call($name, $arguments) {
		$args = implode(',',$arguments);
		log_message('error', $this->modelName.'-> '.$name.'('.$args.') Not exists.');
		return FALSE;
	}

Well, everything is clear here I believe. I think that most of the developers don’t use __call method, but the use I did here is pretty useful.
Each method I have in the models and ofcourse MY_Model using $this->modelName for logging errors – if I didn’t got any $ID for updating some row its highly important for me to know why and where.

Well, I shared with you as much as I could. I don’t know if I’ll write another post in this area, I’ll see how my project goes and if there is something interesting to write about.

I really think I chose the wrong title for this post series :D but that’s ok.

Hope you learned something new, if you have any questions I’ll be happy to answer.

Posted in CodeIgniter, PHP, Web Development at January 1st, 2010. 1 Comment.

Developing Content Managment System With CodeIgniter – Part 2

Hi, if  you haven’t read my previous post, please read it now: Developing Content Management System With CodeIgniter Part 1.

MY_Controller and other extended core libraries

As I told you, I always write my own controllers that extends CI’s Controllers. The method is simple, I write 2-3 Controllers and looking for similarities.

class Add extends Controller {

    public function Add() {
        parent::Controller();
        log_message('debug', 'User / Add Class Initialized.');
    }

    public function index() {
        // Loading the new user form..
    }

    public function process() {
        // Getting the input and activate
    }
}

The example above is a class for creating new user. index() methods loads the view of the new user form, and the process() gets the post data and inserts it to the database.
Controllers like this I have almost in each module I create, so why not writing this MY_Controller class? look:

class MY_Controller extends Controller {
    // The name of the module, User, Catalog, Photo Gallery..
    private $moduleName;

    // Add New Form
    private $addNewView;

    public function MY_Controller() {
        parent::Controller();

    }

    public function index() {
        $content = $this->load->view($this->moduleName.'/'.$this->addNewView, '', TRUE);

        // As I told you before, I create a TPL library that handles all my template stuff.
        $this->tpl->createTemplate($content);
    }

    public function process() {
        // This is the only function I write for each controller like this,
        // this method validates the data and activates the proper model
        // to insert this data in DB.
    }
}

So my new User Add class will look like this:

class Add extends MY_Controller {
    // The name of the module, User, Catalog, Photo Gallery..
    private $moduleName;

    // Add New Form
    private $addNewView;

    public function Add() {
        parent::MY_Controller();
        // Add New Form - the view I have in "views/user/" -
        // remember the module method from the last article?
        $this->addNewView = 'add';

        // This means that the Add.php class is sitting in controllers/user folder.
        $this->moduleName = 'user';
    }

// Because I already implemented index() method in MY_Controller class,
// All I left to do is write my own process() method that is unique for each controller.

    public function process() {
        // In my view, all the inputs of my form named data[xxx] -
        // for example: data[FirstName] and data[LastName], so I grab them as an array.
        $data = $this->input->post('data');

        // Some validation and manipulation..
        $this->user_model->add($data);

        // some more stuff..
    }
}

Conclusion

This concept in great for models too, if you’re not using ORM, you can create basic save() & deleted() methods in your MY_Model class.
The only thing you should do to see if you need this: look for similar code between controllers and models, DO NOT WRITE THE SAME CODE TWICE.

I hope my next part will come faster then this one.

Posted in CodeIgniter, PHP, Web Development at December 19th, 2009. 3 Comments.

Developing Content Management System With CodeIgniter – Part 1

Hi, as i said, I’m stating new project – CMS that developed in CodeIgniter. In this and next few posts I’ll describe the process of developing this app, the infrastructure I create for this app and share some idea with you.

If you haven’t read my Must Have Feature in your CMS post, please do so right now, I will include those features in my application.

My Folders Structure

When you develop an application that is pretty big, and in my idea its big when you have 2 or more modules [like "Photo Gallery" and "eShop"], you must separate the controllers-models-views in order to have understandable application structure.
I find it more useful to put all my controllers of the Photo Gallery module in folder named gallery inside my controllers folder – so my urls will look like: myapp.com/gallery/category/new -> when the gallery is my folder, category is my controller and new is my method inside the controller.
The same thing I’m doing with Views – and with views its highly important because you have a lot of them. So when you load some view in your controller, you use:

$this->load->view($this->moduleName.'/addNewForm', '', TRUE); 

and of course the moduleName is a field that contains the folder name inside the views folder of your module’s views.
(For some reason, doing this with models isn’t working, so my models folder is pretty ugly in all applications I develop).
I decided to look for the problem with this folder structure in my models – I noticed that I didn’t loaded them correctly, now I see that this structure works for models too.

Template system

My template system is always pretty easy. I use the load->view functionality [for those who aren't familiar with CodeIgniter, load->view is basically loading HTML file and pass an array of data to it - the array extracted and data inserted in the right place in your HTML file] but differently then what is shown in CodeIgniter’s videos.
I create a library – class named TPL, this class loads my basic template of the application. The array contains my content in the content area, the content in the sidebar and etc.
Usually I use a clean design too [for LightWindow for example], or maybe there is certain areas where I don’t need to show the sidebar, or maybe I supposed only to show the content itself that passed to this class. So my class is strong enough to know witch view it supposed to load each time -> maybe with uri segments? or maybe with params I send to the TPL class?

Each method in my controllers creates eventually the content by loading its own views, but I never load the main view from the controller directly! I use the TPL class for it. I hope you understand why – mainly because of code duplication, its not scalable, I have a lot of items in my design [tabs, menus, tree navigation...] and its really important to control all of these and doing it in one place.

Consider this simple idea for your applications, for now – it did everything I needed.

MY_Controller

CodeIgniter allows you to extend core libraries, and I use it to extend models and controllers. In the future posts I’ll explain deeply about what actually I do, but here I want you to get the idea.
Almost in each Controller in your application you have the same methods and basic functionality, you have an items list, you have the Add New Item form, the method that inserts the data in your database.. there is a chance this code might be duplicated, and I wish to prevent it, write good code :)

In my controller, I add these basic methods and create a basic infrastructure for them to work in each of my controllers. In my next post I’ll explain it.. Until then I wish to hear your opinion!

Posted in CodeIgniter, PHP, Web Development at November 25th, 2009. 4 Comments.

New facebook worm

Hi, few moments ago I received a message on facebook:

New Facebook Worm

New Facebook Worm

Make sure you not entering this url! It redirects to a blog at blogspot.com and then to porn webpage.

Posted in General Talk at November 14th, 2009. No Comments.

Must have features in your CMS

Hi!

I’ll start in few days new project, for this project I need to develop brand new CMS that will be used for a lot of websites. This got me thinking about the basic features I need to include in order to make my life easier, so this is what I have so far:

1. BackOffice for the BackOffice

Create 2 levels of administration, 1 for the client and yourself, and the other one is for you only. In your administration  level you can add some tools for yourself that make the work easier, Some of those features I’ll describe below.

2. Protected Items

This is something you can add to your administrative level. Protected Items is those content pages/catalog categories and etc that your cms work with. For example if you created a content page that acts like “Thank you for shopping in SomeShop”, and you redirect to this content page through your code you don’t want the client to delete it by mistake. You got the idea? Lock the option for the simple admin to delete these important items.

3. Modules On/Off

The CMS I have in mind contains Photo Galley, Contact Form, Content Pages, Calendar and etc. Each of these are modules that are ready to be “installed” in each of my new clients. I install for him the whole CMS I have [with those modules I listed] but enabling only those he paid for, and if suddenly he needs something else I already have – just by marking the checkbox he’ll have the new module hw wanted – already integrated with his site. And yes – this is something you put in your administrative level.

4. System Logs

I work with Ubuntu and 9 workspaces, each workspace has its own purpose. 1 of my workspaces shows the logs of the applications I working on. But getting into those logs just because the client told me he had some error in some page at some point in the day is not that fun, thats why I’m creating a page in the CMS that shows me the logs so I could see really fast and more comfortable the problem he had. And by the way – telling to the client what exactly he did that gave him this error show you have control over the product you develop – and its really impressive.

5. Duplicate Items

The basic objects management  is Add/Edit/Delete of content page of catalog item. I really would like you add the “create a copy” of each of those items. I don’t have any good reason for that yet, But since I adding it to each application I’m building I finding myself actually using it and saving some time to myself.

6. System Restore

This is maybe the strongest feature I can think of. Keep track of each table in your database, and you’ll be able to get back in time with the data of your application. Create a simple table named “db_history” that contains the fields: tableName, fieldName, fieldValue, fieldNewValue and nowTime.
The names are pretty clear I guess, the nowTime is to know when the change has been made. With this basic feature you can do a lot and it will help you solve many different problems and client’s issues.

I’ll be happy to see your suggestions for features like these, feel free to post a comment!

P.S
This features can be helpful to you in any application you develop, not only CMS-type application.

Posted in Business, CodeIgniter, General Talk, Insights, PHP, Web Development at November 13th, 2009. 4 Comments.

Sorry for not updating

Hi there! I’m sorry I didn’t updated my blog lately, I’ve been very sick in the last few days. I’m feeling much better now, hope the headache will go away entirely.

Posted in General Talk at November 7th, 2009. No Comments.

“Site Maintenance”, or – How to keep getting paid after the job is done

The normal workflow as a freelancer [At least for me] is build new website [by modifying existing CMS or building new one], guide the client to use your CMS and then move on to the next project.

Lets say you client is CEO of some company, or maybe runs little lawyers office. He don’t really has the time to  maintain he’s website, so he got 2 choices:

  1. Hire some one to add/remove/edit content in the new website [most of the time it may be son of some friend or even new employee at the office]
  2. Let existing employee do this job.

OR! you can offer him 3rd choice. The site maintenance service.

Site Maintenance

The “Site Maintenance” service, as I see it, meant only to keep getting paid each month. This is quite smart too – you develop this kick-ass CMS, install it and modify it so it fits your new client, then you offer him your special service – site maintenance. All you do in this service is getting content changes and modifications from your client, use your kick-ass CMS to apply them. And get paid for it.
[This service may include also SEO]

Really smart.
The client already knows you, he trusts you, you know how to work with the CMS you’ve built, then you are a natural option. You just need to sell him this service right.

There is several methods for this service to work, you can get paid each month for X dollars for SEO and Content Management, or you can get paid per-hour for the job you getting done each month.

Expand your business

This service is great for expanding you business.
Lets say you charge 250$ for maintenance service, you hire other freelancer [or if it only content management within your system then even your little brother can do this] pay him 100$ for this job you pass him each time, and your clean revenue is 150$.

Even if you don’t want to expand – this option lets you spend some more time on doing whatever you want and still get paid.

Why I don’t do it anymore

Just so you know, in the last few years I stopped on working hard in the webdev industry. The main reason is the time – I’m a soldier now, so I can only work half-day. This half day I rather spend on my projects then working on my business. Managing people can be hard. So if I build a website for a client, I guide them to use the CMS rather then maintain a site by myself. This way I can move on, working on a different projects and not being held back by my old projects.

Posted in Business, Insights, PHP at November 3rd, 2009. 2 Comments.

Complete Google Wave Guide

Hi! Mashable linked in hid twitter to http://completewaveguide.com -> This site has a lot of useful info about Google Wave! Recommended!

Posted in General Talk at November 1st, 2009. No Comments.
Get Adobe Flash playerPlugin by wpburn.com wordpress themes