4

I'm using Plupload to manage file uploads for my site. When I configure Plupload to post to the following test file, the records are shown correctly, however when I post to a CI controller, both $_POST and $_FILES are empty.

test.php

<?php print_r($_FILES); print_r($_POST); ?>

CI does correctly display the $_FILES and $_POST arrays when using a standard HTML form, so any ideas what's causing this?

EDIT here is the plupload config

    var uploader = new plupload.Uploader({
    runtimes : 'html5,html4,flash,silverlight,browserplus',
    browse_button : 'pickfiles',
    container : 'container',
    max_file_size : '15mb',
    url : '/test.php',
    //url : '/upload/do_upload/',
    flash_swf_url : '/js/plupload/plupload.flash.swf',
    silverlight_xap_url : '/js/plupload/plupload.silverlight.xap',
    filters : [
        {title : "Documents", extensions : "pdf,doc,docx,rtf,txt"}
        ],
    multipart_params : { job : -2 }
});

and here is the controller

class Upload extends CI_Controller {

function __construct()
{
    parent::__construct();
}

function index()
{
    $this->load->view('upload_form', array('error' => ' ' ));
}

function do_upload()
{
            print_r($_POST);
            print_r($_FILES);
    $config['upload_path'] = 'incoming/';
    $config['allowed_types'] = 'pdf|doc|docx|rtf|txt';
    $config['max_size'] = '900';

    $this->load->library('upload');
    $this->upload->initialize($config); // MUST CALL ELSE config not loaded

    if ( ! $this->upload->do_upload()) {
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('upload_form', $error);
    }
    else {
        $data = array('upload_data' => $this->upload->data());
                    $this->load->model('translation_model');
                    $this->translation_model->add_orig($job, $filePath);
        $this->load->view('upload_success', $data);
    }
}
}
shaedrich
  • 5,457
  • 3
  • 26
  • 42
AlasdairC
  • 190
  • 1
  • 1
  • 14
  • 2
    AFAIK that Codeigniter empties the `$_POST` array and uses `$this->input->post` for reference and security. For file uploads you can use the File Uploading library. – MacMac Feb 20 '12 at 14:22
  • No, Codeigniter does NOT empty the `$_POST` array. Why are people saying that? CI does unset `$_GET` *only* if you disable query strings. – Wesley Murch Feb 20 '12 at 15:26
  • Thanks for posting your code. How are you verifying that $_FILES and $_POST are empty? Are you just *assuming* it because the upload doesn't work? You need to use plupload's upload script or a variation of it, you cannot simply interchange CI's upload handler with plupload's. – Wesley Murch Feb 20 '12 at 15:53
  • @Madmartigan sorry I've updated the controller - if I send files with multipart : true, and disable chunking, isn't plupload just uploading the files in one go as a regular multipart form upload, php stores in /tmp. If I have plupload POST to my test.php form this is exactly what happens. – AlasdairC Feb 20 '12 at 15:59
  • In any case, I couldn't tell you why $_POST/$_FILES is empty, it shouldn't be. Try with a bare-bones plupload config and debug `$_REQUEST`, see what happens. – Wesley Murch Feb 20 '12 at 16:26

4 Answers4

5

Most probable cause for this is bad mod rewrite rules. If you are using any, try disabling them and post your data again.

Repox
  • 15,015
  • 8
  • 54
  • 79
  • `$_POST` will work fine in Codeigniter, not sure what you mean by `"CI cleans the post array"`. If you mean that CI unsets the $_POST array, you are mistaken. If you read the OP's question this should be clear: `"CI does correctly display the $_FILES and $_POST arrays when using a standard HTML form"` – Wesley Murch Feb 20 '12 at 15:32
  • 1
    Could be related: http://stackoverflow.com/questions/6079371/updated-mod-rewrite-and-post-method-in-php-problem-post-is-always-empty, http://stackoverflow.com/questions/4733123/why-post-is-empty-when-using-qsa-in-htaccess, http://stackoverflow.com/questions/1627017/files-array-in-php-is-empty – Wesley Murch Feb 20 '12 at 18:15
  • Argh, I have modrewrite enabled inorder to tidy the site's urls - I'll test and get back. thanks for the tip – AlasdairC Feb 20 '12 at 19:14
1

I'm using the input class in conjunction with regular $_POST variables and it has always worked for me. The theory that CI cleans out post variables doesn't add up when he can dump them in regular forms.

Turn off csrf protection and please let us know the results. Use firebug console to read the answer from the server. For a workaround regarding csrf I use https://github.com/EllisLab/CodeIgniter/pull/236

CodeIgniter also has issues with file types. http://codeigniter.com/forums/viewthread/113029

I have the working libraries for both, if you want them just drop me a message.

qwertzman
  • 784
  • 1
  • 9
  • 23
0

First of all, you should check that you have a form tag for enctype.

add enctype="multipart/form-data" to form as it supports uploading.

It will work, can you try and put both the print_r function after else tag before $data, It will run once the if () condition satisfies. So add it in the else tag and run it.

function do_upload()
{
            
    $config['upload_path'] = 'incoming/';
    $config['allowed_types'] = 'pdf|doc|docx|rtf|txt';
    $config['max_size'] = '900';

    $this->load->library('upload');
    $this->upload->initialize($config); // MUST CALL ELSE config not loaded

    if ( ! $this->upload->do_upload()) {
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('upload_form', $error);
    }
    else {
           print_r($_POST);
            print_r($_FILES);
        $data = array('upload_data' => $this->upload->data());
                    $this->load->model('translation_model');
                    $this->translation_model->add_orig($job, $filePath);
        $this->load->view('upload_success', $data);
    }
}
shaedrich
  • 5,457
  • 3
  • 26
  • 42
0

It could be that something in plupload is intercepting $_POST and $_FILES.

In codeigniter it's usually better to use the input class to deal with post variables and the file uploading class to deal with file uploads.

I guess that something has emptied the $_POST and $_FILES array for safety, it could be that on including the input and file uploading classes codeigniter itself wipes the $_POST and $_FILES arrays so that you're forced to use their classes. It's always a good idea as codeigniter removes XSS attacks and cleans up variables.

shaedrich
  • 5,457
  • 3
  • 26
  • 42
Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224