• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using SWFUpload + Sessions + upload class, how I did it.

Newer Version HERE! http://ellislab.com/forums/viewthread/221152/

Hello everyone. I have been a member for some time, and I have used the forums many times to solve many a problem, but I haven't really contributed much. I usually just find my solution and move on.
SwfUploader is something I have been playing around with for a while now, but never did find a solid implementation to keep the sessions when I uploaded files. Well I finally figured out a solution that fits me, and I think works very well. I thought I would post about it and maybe help someone else out, considering this has been a fustrating topic for many. So here is my implementation:

Let me start out by saying that you must be using the native session library. It can be found in the wiki. By allowing your sessions to be handled by php, you can pick up the session in from the "flash" useragent.

I modified the native session library. I figured it was ok, considering by using the native session library, I am still not changing the core code.

First of all, to use the swfUploader with the code igniter upload class, you need to add the appropriate mime types.

'jpeg'    =>    array('image/jpeg', 'image/pjpeg', 'application/octet-stream'),
'jpg'    =>    array('image/jpeg', 'image/pjpeg', 'application/octet-stream'),
'jpe'    =>    array('image/jpeg', 'image/pjpeg', 'application/octet-stream'),
For every file type that you need to be able to upload with swfuploader, you need to add the 'application/octet-stream' mime type to the file extesion in mimes.php. Here, I added the mime type to jpeg, jpg, and jpe. This is just because it is how swfupload uploads the data.

Next you need to modify the native sessions library
what we will do is enable a paramater for the session id, and if the session id is set, then we will tell the php session to use that session id, instead of trying to create a new one for the new useragent when flash accesses the upload script.

/system/application/libraries/Session.php (roughly line 31)
//Add $params = array() as a parameter
//so if theres no entry (like the default way of calling it) it will just default
//to an array
function CI_Session($params = array())
    $this->object =& get_instance();
    log_message('debug', "Native_session Class Initialized");

    //were looking for $params['session_id']. if its set, then we want to
    //start the session with that session id. we need to pass it to the function
    //that starts the session.
        log_message("debug", "Session ID is passed, using ".$params['session_id']);
    } else {
        //and if its not set, well start the sessions to classic way.
            log_message("debug", "Figuring Out Session ID automagically");
I placed all of my edits in the comments. Basically, i just added a parameter, and if that parameter is set, I pass it to the the method that actually starts the session, so we will need to edit that method as well, in the same file. Also note, I sprinkled in some "log_messages" this can be handy as you wont be able to see output of the upload script used by swfuploader. so you can watch the logs if you enable them, to debug your script.

/system/application/libraries/Session.php (roughly line 146)
//again, here, I add a parameter $phpsessid and give it a default of null, so it is reverse
function _sess_run($phpsessid = null)
        //if the paramater is NOT null (something was passed to it)
            //then we will tell php to use the session id that was passed
            if ($phpsessid != null){
           log_message("debug", "setting session id to use: ".$phpsessid);
           //this is where the magic happens

        $session_id_ttl = $this->object->config->item('sess_expiration');
Here, as you see, (i added comments to what I changed) I added a paramater to the method.
if the paramater is set (it should be a session id) then we tell php to start the session using that session id.

Now you can use the current session data with swf upload while using the upload class, which I will also demonstrate.

The Controller (/system/application/controllers/fileupload.php
class Fileupload extends Controller {

    function Fileupload()

    function index(){
        //load helpers
        $this->load->helper(array('url', 'form'));
        //load session library the normal way


    function upload(){
       //you should have swfuploader POST your session id (youll see in the view)
       $params['session_id'] = $this->input->post("PHPSESSID");
       //load the session library the new way, by passing it the session id
       $this->load->library('session', $params);
       $this->load->library('upload') // load the upload class

       //now if the session was posted, and it loaded correctly, you will have
       //access to your session !
       if ($this->session->userdata('logged_in') != true) show_404(); //cancel if the user is not logged in

        $user_id = $this->session->userdata('user_id');

        $config['upload_path'] = $_SERVER['DOCUMENT_ROOT'].'/assets/uploads/';
        //make sure this directory exists and is writeable!
        $config['allowed_types'] = 'jpg';
        $config['max_size']    = '10240';
        if ( ! $this->upload->do_upload("Filedata")){
             //upload failed, do stuff
             show_404(); //maybe? you can handle it how you want with swfuploader
        } else {
             //insert stuff to a database.

Messages In This Thread
Using SWFUpload + Sessions + upload class, how I did it. - by El Forum - 11-19-2008, 02:07 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.