• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multi Upload with CI & swfUpload

[eluser]Gabriel Popa[/eluser]
i founded the problem. it was
part of pdf mime time missing

[eluser]Gabriel Popa[/eluser]
thank you ... a lot.. have a nice we.

i have some session problems...
here is my view
<link rel="stylesheet" type="text/css" href="<?=base_url();?>assets/css/default.css">

        var swfu;

        window.onload = function() {
            swfu = new SWFUpload({
            //var settings = {
                flash_url : "<?=base_url();?>assets/js/swfupload_f9.swf",
                upload_url: "<?=base_url();?>index.php/projects/dominiuploadx",    // Relative to the SWF file
                post_params: {"PHPSESSID" : "<?=$this->session->userdata('session_id');?>"},
                file_size_limit : "64 MB",
                file_types : "*.*",
                file_types_description : "All Files",
                file_upload_limit : 100,
                file_queue_limit : 0,
                custom_settings : {
                    progressTarget : "fsUploadProgress",
                    cancelButtonId : "btnCancel",
                    load_target : "divFileProgressContainer"
                debug: true,

                // The event handler functions are defined in handlers.js
                file_queued_handler : fileQueued,
                file_queue_error_handler : fileQueueError,
                file_dialog_complete_handler : fileDialogComplete,
                upload_start_handler : uploadStart,
                upload_progress_handler : uploadProgress,
                upload_error_handler : uploadError,
                upload_success_handler : uploadSuccess,
                upload_complete_handler : uploadComplete,
                queue_complete_handler : queueComplete    // Queue plugin event

            //swfu = new SWFUpload(settings);

<div id="content">
    &lt;form id="form1" action="&lt;?=base_url();?&gt;index.php/projects/miniuploadx" method="post" enctype="multipart/form-data"&gt;
            <fieldset class="flash" id="fsUploadProgress">
            <legend>Upload Queue</legend>
        <div id="divStatus">0 Files Uploaded</div>
                &lt;input type="button" value="Upload file" onclick="swfu.selectFiles()" style="font-size: 8pt;" /&gt;
                &lt;input id="btnCancel" type="button" value="Cancel All Uploads" onclick="swfu.cancelQueue();" disabled="disabled" style="font-size: 8pt;" /&gt;

controller projects.php
function miniuploadx(){
     $config['upload_path'] = 'upload/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '10000';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        $this->load->library('upload', $config);

        //load view

function dominiuploadx(){
        log_message('debug', explode(',' , $_POST) );

        $res = $this->upload->data();

        echo $res['file_name'];


SWF DEBUG: SWFUpload Init Complete
SWF DEBUG: Build Number:           SWFUPLOAD 2.1.0 FP9 2008-05-12
SWF DEBUG: movieName:              SWFUpload_0
SWF DEBUG: Upload URL:             http://www.domain.tld/index.php/projects/dominiuploadx
SWF DEBUG: File Types String:      *.*
SWF DEBUG: Parsed File Types:      
SWF DEBUG: File Types Description: All Files (*.*)
SWF DEBUG: File Size Limit:        67108864 bytes
SWF DEBUG: File Upload Limit:      100
SWF DEBUG: File Queue Limit:       100
SWF DEBUG: Post Params:
SWF DEBUG:                         PHPSESSID=85ab2946a8fb7c92b125fe85400c44de
SWF DEBUG: Event: fileDialogStart : Browsing files. Multi Select. Allowed file types: *.*
SWF DEBUG: Select Handler: Received the files selected from the dialog. Processing the file list...
SWF DEBUG: Event: fileQueued : File ID: SWFUpload_0_0
SWF DEBUG: Event: fileDialogComplete : Finished processing selected files. Files selected: 1. Files Queued: 1
SWF DEBUG: StartUpload: First file in queue
SWF DEBUG: Event: uploadStart : File ID: SWFUpload_0_0
SWF DEBUG: Global Post Item: PHPSESSID=85ab2946a8fb7c92b125fe85400c44de
SWF DEBUG: ReturnUploadStart(): File accepted by startUpload event and readied for upload.  Starting upload to http://www.domain.tld/index.php/projects/dominiuploadx for File ID: SWFUpload_0_0
SWF DEBUG: Event: uploadProgress (OPEN): File ID: SWFUpload_0_0
SWF DEBUG: Event: uploadProgress: File ID: SWFUpload_0_0. Bytes: 32768. Total: 71189
SWF DEBUG: Event: uploadProgress: File ID: SWFUpload_0_0. Bytes: 65536. Total: 71189
SWF DEBUG: Event: uploadProgress: File ID: SWFUpload_0_0. Bytes: 71189. Total: 71189
SWF DEBUG: Event: uploadSuccess: File ID: SWFUpload_0_0 Data:

Any suggestion? what did i do wrong?

Can you let us know what you mean by session problems?

Just adding the "PHPSESSID" to your SWFUploader does not really do anything, you need to build in logic on the backend to map that session id back to a known user....

for example...

Bob has the session of 123ABC
Bob uses SWFUpload to send a file, we include PHPSESSID=123ABC
SWFUpload posts the form and uploads the data, but because of the way flash handles the upload, the flash app part gets it's own session id of XYZ789.

When the form is submitted, the controller processing the form needs to pull the $_POST['PHPSESSID'] value of ABC123 and map it back to the user Bob. It can pretty much ignore the session id of XYZ789, as it's not needed.

How you get your application to map the session id back to Bob is the tricky part. Smile

For me, I don't actually use the session id; gave up on this. I assign each user a unique 64bit APIKey that they use for uploading. If this value matches, they are golden to upload files. However, if someone else gets a hold of a person's APIKey, then they can upload file to the users account. I give the users a way to change their API key at will too. Smile I'm dealing with a low security app, your mileage may vary depending on your requirements.
if( isset($_POST['APIKEY']) ){
     $getuser = "SELECT username, user_id, user_bucket, site_apikey
                 FROM ht_users
                 WHERE site_apikey = ? ";
     $query = $this->db->query($getuser, array($_POST['APIKEY']) );
     $userdata = $query->row_array();

Hope this helps

@w3builder: Where are these functions declared?


Can anyone show me what these event handlers are supposed to do?

Please ignore this post, I just discovered encrypt_name which works great!

Great example and is working fine.

I would like to save the uploaded file with a unique name. I can create the unique name with my own function but am struggling to assign this unique name to the file when it is uploaded.

Any suggestions greatly appreciated.


[quote author="Merolen" date="1204688480"]I've been trying to make SWFUpload to work with CI as well. After using this post and the wiki entry as a startingpoing I've got a working sample.

Here is my experience:

I've used both the handler.js, swfupload.js, main.css and the images from the demo. I've used absolute path, guess it works fine with relative as well. This is put within the &lt;head&gt; tags:
&lt;link rel="stylesheet" type="text/css" href="/absolute/path/to/css/default.css" /&gt;
&lt;script type="text/javascript" src="/absolute/path/to/handlers.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="/absolute/path/to/swfupload.js"&gt;&lt;/script&gt;

Then the SWFUpload initialization, this is copied from the demo. Note that this has to be within the &lt;head&gt; tags. Here I've given the URL, the demo uses relative path. But this works for me:
var swfu;
        window.onload = function () {
            swfu = new SWFUpload({
                // Backend Settings
                upload_url : "http://www.mysite.com/upload/do_upload",
                flash_url : "http://www.mysite.com/path/to/swfupload_f9.swf",
                // Don't think this is needed
                post_params: {"PHPSESSID": "&lt;?php echo session_id(); ?&gt;"},

                // File Upload Settings
                file_size_limit : "2048",    // 2MB
                file_types : "*.jpg",
                file_types_description : "JPG Images",
                file_upload_limit : "0",

                // Event Handler Settings - these functions as defined in Handlers.js
                //  The handlers are not part of SWFUpload but are part of my website and control how
                //  my website reacts to the SWFUpload events.
                file_queue_error_handler : fileQueueError,
                file_dialog_complete_handler : fileDialogComplete,
                upload_progress_handler : uploadProgress,
                upload_error_handler : uploadError,
                upload_success_handler : uploadSuccess,
                upload_complete_handler : uploadComplete,

                custom_settings : {
                    upload_target : "divFileProgressContainer"
                // Debug Settings
                debug: false

Now the code in the controller. My class is called upload and the function is called do_upload, the function must not be confused with CI's own do_upload function which is used inside our own do_upload. The most important thing to notice here is the echo at the bottom. If nothing is echoed the upload will just hang. The echo is the way to send information to the handler.js. Here the filename is echoed so that the image can be shown when the upload is finished.

class Upload extends Controller {
    function Upload() {
        $config['upload_path'] = './relative/to/root/upload';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '1000';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        $this->load->library('upload', $config);
    function do_upload() {
        $res = $this->upload->data();

        echo $res['file_name'];

And now to the view file which will show the upload button
<div style="margin: 0px 10px;">
        &lt;button id="btnBrowse" type="button" style="padding: 5px;" onclick="swfu.selectFiles(); this.blur();"&gt;
            Select Images <span style="font-size: 7pt;"></span></button>
    <div id="divFileProgressContainer" style="height: 75px;"></div>
    <div id="thumbnails"></div>

So the handler.js. You want to have a look at the function uploadSuccess where the AddImage function is called. Change the 'thumbnail.php?id=' + server_data to '/path/to/your/pic/' + server_data. Server_data is what we echoed in the do_upload function. Also just scroll through the file to change the text messages and gif urls.
For debugging I echoed information from $this->upload->data(); and wrote alert(server_data) before the AddImage function is called in uploadSuccess.

Remember to fix the mime-type in system/application/config/mimes.php and add 'application/octet-stream'. Some say that this may be a security risk, I don't know, but it has to be done for it to work.

'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'),
'png'    =>    array('image/png',  'image/x-png', 'application/octet-stream'),

I hope this helps.[/quote]

If this helps anyone (I'm still working on fixing my problem):

SWFUpload, at least here, seems to insist on putting the post_params also into a GET, appended to the URL you're specifying.

For example: http://your.site/script/foo/?PHPSESSID=f...f562r3f67e

You can see this in your apache2 access_log if you have access to it.

This ?PHPSESSID= of course is being sanitized by CI and therefore throws back a big middle finger to SWFUpload, even though you see the whole file being posted.

Seems like you _have_ to use the Flash 9 version of the swf.

That way, you can set use_query_string:false.

That way, SWFUpload doesn't set GET strings at the end of the URL that cause CI to refuse the request.

Having said that, CI's session library and any Flash frontend to uploads are simply not meant to be together. It's based on cookies, which is discarded by Flash as an intermediary.

You can go back to PHP's own session system and still use the same code you have in CI now, if you replace its session library by the one documented here: http://codeigniter.com/wiki/Native_session/

If you want to keep CI intact however, work around it instead, like other users have. By passing some kind of key to SWFUpload that you keep in your database for each of your users. Then look for the right user based on that key.

I've just found this thread after having session problems myself with several flash upload components.

For anyone else having the same problems, you may be able to get around it by setting
$config['sess_match_useragent'] = FALSE;
in your config file.

You see, the CI session manager is getting a different user agent string from your flash component and destroying the session.

aha, good thinking batman - will do.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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