EllisLab text mark
Advanced Search
1 of 2
1
   
Guide: CodeIgniter + uploadify + Session Cookie (Flash)
Posted: 27 April 2012 03:56 PM
Avatar
Joined: 2011-01-24
149 posts

Hi there,
I’m writing my first guide about how to make uploadify work together with CodeIgniters Session library. I’ve had this problem once and had a hard time to figure out how to make it work.

The problem: Flash (which is used by uploadify) is not able to send the common browser cookie to the server, it creates it’s own one. Therefore you cannot upload files using your already established session.

The solution:
This guide has been written for CI 2.1.0, it can easily be edited for prior versions.

EDIT
-In later versions of uploadify post_params in JavaScript may be replaced by formData

1. Extend CI_Session
Create a MY_Session.php under /application/libraries or extend your own one. Copy the whole sess_read() function from /system/libraries/Session.php and paste it into your newly created MY_Session.php.

Replace:

$session $this->CI->input->cookie($this->sess_cookie_name); 

With:

if (isset($_POST['browser_cookie']))
  
{
   $_cookie 
$this->CI->input->post('browser_cookie');
   
$enc_key $this->CI->config->item('encryption_key');
   
$session trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256$enc_keybase64_decode($_cookie), MCRYPT_MODE_ECBmcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256MCRYPT_MODE_ECB), MCRYPT_RAND)));
  
else {
   $session 
$this->CI->input->cookie($this->sess_cookie_name);
  

2. Session Library Setup
Setup your Session Library properly (add an encryption_key in your config.php) and enable table based sessions as well as disable sess_match_useragent in config.php (important!)

3. In your view where uploadify is embedded, you will have to append post params to your upload function. Here is a sample code:

$('#do_upload').uploadify({
  swf
'/js/uploadify/uploadify.swf',
  
uploader'/objects/upload_images/' object_id,
  
post_params{"browser_cookie""<?= trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->config->item('encryption_key'), $_COOKIE[$this->config->item('sess_cookie_name')], MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)))); ?>"},
  
cancelImage'/js/uploadify/uploadify-cancel.png',
  
checkExistingfalse,
  
multitrue,
  
autotrue,
  
onUploadSuccess: function(filedataresult){
     
//do something useful
  
}
 }
); 

This has to be parsed by your PHP parser in order to fill the values. You could also just declare a variable like

var browser_cookie = “<?= trim…....... ?>”

and just load this global variable in your upload function.

4. How does it work?
Basically it generates an encrypted version of your session cookie and posts it to your server. As you have already modified the session class, it recognizes that your session cookie has been sent via post and is using this instead. Basically it’s just a simulation of what the browser would do if it wasn’t Flash.

5. Conclusion and caveats
If CI Basis is updated, don’t forget to update your MY_Session as well. It is just ensuring that.

Make sure that your cookie is valid before sending the files to your server. Otherwise you could raise the value of “sess_time_to_update” in your config.php.

Looking forward to your feedback.

 Signature 

Foreign source code is like a minefield…

 
Posted: 27 April 2012 09:55 PM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2010-09-24
240 posts

This is great, thanks for the guide. Implementing Uploadify to CI is a ball of pain. I’ll try this and report back.

 Signature 

Taskhub - Your local Marketplace for tasks and errands

 
Posted: 08 May 2012 10:55 AM   [ # 2 ]   [ Rating: 0 ]
Joined: 2012-05-08
3 posts

Where is het variable “object_id” coming from in the line “uploader: ‘/objects/upload_images/’ + object_id,”?
It’s giving me a javascript error for an undefined variable…

 
Posted: 08 May 2012 11:00 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2011-01-24
149 posts
kneep - 08 May 2012 10:55 AM

Where is het variable “object_id” coming from in the line “uploader: ‘/objects/upload_images/’ + object_id,”?
It’s giving me a javascript error for an undefined variable…

This is just an example, copied right out of my application. You can set it to your upload controller function.

 Signature 

Foreign source code is like a minefield…

 
Posted: 08 May 2012 11:15 AM   [ # 4 ]   [ Rating: 0 ]
Joined: 2012-05-08
3 posts

Can you post an example of your upload controller to?

 
Posted: 08 May 2012 12:02 PM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2010-09-24
240 posts

I’ve been struggling to reproduce it as well, it’d probably be useful if you posted a full working example on Github or such.

 Signature 

Taskhub - Your local Marketplace for tasks and errands

 
Posted: 08 May 2012 05:12 PM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2011-01-24
149 posts

I’ll upload a test case tomorrow. Stay tuned smile

 Signature 

Foreign source code is like a minefield…

 
Posted: 09 May 2012 03:46 AM   [ # 7 ]   [ Rating: 0 ]
Joined: 2012-05-08
3 posts

I’m using the OBSession library, so the above ‘session’ script will not work with that, have you used that somewhere?

 
Posted: 14 May 2012 10:32 AM   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2010-09-24
240 posts

Just bumping this to encourage Stefan to post a working example.

 Signature 

Taskhub - Your local Marketplace for tasks and errands

 
Posted: 14 May 2012 04:07 PM   [ # 9 ]   [ Rating: 0 ]
Joined: 2009-09-10
90 posts

Great example. I found various methods a bit hackish with uploadify so i change to Plupload and this works wonders with no modification to the session.

 
Posted: 14 May 2012 07:30 PM   [ # 10 ]   [ Rating: 0 ]
Avatar
Joined: 2011-01-24
149 posts

I have not forgotten my commitment smile I’m just having a bit of vacation. Will be back at friday

 Signature 

Foreign source code is like a minefield…

 
Posted: 31 May 2012 02:35 AM   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2009-01-13
34 posts

I have almost wasted 150$ worth of my time in implementing uploadify with CI, it just doesn’t work. Here is my use case which I will be addressing with plain old JS
* Let user create a gallery with dimensions for thumbnails using a simple form
* Allow user to upload images in that same form
* On form submit, create a gallery entry in db, if failed inform user
* If prev step passes, create a sub folder for that gallery in a designated folder, in this gallery folder create thumbs folder
* Create thumbs out of original and place thumbs in thumbs dir and original in respective gallery folder
* Update db with every upload.
* All this has to be portable, and should take into account upload restrictions on a server and php settings

But still keeping track of this thread.

 Signature 

LAMP Development Blog
Shameless Self Promotion

 
Posted: 04 June 2012 09:56 AM   [ # 12 ]   [ Rating: 0 ]
Avatar
Joined: 2010-09-24
240 posts

Likewise, I haven’t been able to reproduce a working sample from the info in this thread.

 Signature 

Taskhub - Your local Marketplace for tasks and errands

 
Posted: 04 June 2012 01:30 PM   [ # 13 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
4324 posts

I wouldn’t rely on a flash upload anyway, unless people being able to use your app on iPads/iPhones isn’t a concern to your project.

 Signature 
 
Posted: 04 June 2012 02:08 PM   [ # 14 ]   [ Rating: 0 ]
Avatar
Joined: 2009-01-13
34 posts

The only thing I wanted to implement was to let users upload multiple files using something like AJAX. I could do client side stuff using jQuery, limited the number of uploads to limit set by server, I display the max upload size allowed by server and show the file size as soon as file is selected/browsed, user is warned as soon a file limit is met, I just need to warn user of max upload size.

 Signature 

LAMP Development Blog
Shameless Self Promotion

 
Posted: 10 July 2012 04:23 PM   [ # 15 ]   [ Rating: 0 ]
Avatar
Joined: 2012-03-16
9 posts

I’d love to see the controller the OP is using.

 
1 of 2
1