• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Form Data and File Upload

Well you're sniffing around issues that are a bit dodgy in CI. I don't think there is a easy all-out-CI-way to work this. However, with a little help from the core php library you should be fine.

Well, I tried
  // Do upload

and it still errors out.

It seems like the issue is happening with the validation object(s).

[quote author="wowdezign" date="1259698637"]It seems like the issue is happening with the validation object(s).[/quote]

Exactly how did you incorporate it in your code..?

   //put the code that's doing the upload here

Okay. I got a solution. There are probably other solutions, but I can explain why it was failing for me.

The upload field gets put into $_FILES even if it is empty and the error and size are set to 4 and 0 respectively Here is the do_upload function from CI:
function do_upload($field = 'userfile'){
    // Is $_FILES[$field] set? If not, no reason to continue.
    if ( ! isset($_FILES[$field])){
        return FALSE;
    // Is the upload path valid?
    if ( ! $this->validate_upload_path()){
        // errors will already be set by validate_upload_path() so just return FALSE
        return FALSE;
    // Was the file able to be uploaded? If not, determine the reason why.
    if ( ! is_uploaded_file($_FILES[$field]['tmp_name'])){
        $error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];
            case 1:    // UPLOAD_ERR_INI_SIZE
            case 2: // UPLOAD_ERR_FORM_SIZE
            case 3: // UPLOAD_ERR_PARTIAL
            case 4: // UPLOAD_ERR_NO_FILE
            case 6: // UPLOAD_ERR_NO_TMP_DIR
            case 7: // UPLOAD_ERR_CANT_WRITE
            case 8: // UPLOAD_ERR_EXTENSION
            default :   $this->set_error('upload_no_file_selected');
        return FALSE;
When I submitted my form with an empty upload field I got the following for $_FILES:
    [userfile] => Array
            [name] =>
            [type] =>
            [tmp_name] =>
            [error] => 4
            [size] => 0
So the solution for me was to do:
if($_FILES['userfile']['error'] != 4){
  // Do upload here
Like I said before. There are probably better ways to do this, but My form is working now. Hopefully I won't find something I overlooked later and get :red:.

My thanks to you überfuzz for putting me on the right track. I hadn't thought to look to the $_FILES superglobal.

I was missing the obvious. Namely that the $_POST and $_FILES are separate even though they both are submitted through a form.

DUH! :roll: .

Sometimes my brain cramps! Confusedhut:

Oki, I guess you could ease down my curiosity then. (To lazy to test it!) Is the $_FILES array sent if the user chose to skip the file input in the form?

In my case it was being sent every time. I don't know if it was something in the CI code or if that is how it always is with a file field in a form (haven't really looked into it).

Just happy it works now. :-)

[eluser]Jeremy Gimbel - Conflux Group[/eluser]
Looks like you guys have been busy today.

My thoughts were along the same lines as überfuzz's. But yeah, it's not something I've actually tried. It would seems that the File Upload class could use an extension to incorporate some sort of method that enables you to check for uploaded files without accessing the $_FILES superglobal.

Good work.


my solution is:
if($_FILES['news_img']['name'] != NULL)
   //do your upload code

It's ok for me, tested.

HELP please...

i got also the same problem in this thread...

i read and follow all your discussion here but it had'nt work in my form..

please help me..

here's my code


<div class="infield">
                                    &lt;?php echo form_open_multipart('at_reports/post');?&gt;
                                    &lt;input type="file" name="userfile" class="tb" title="&lt;?=lang('icon');?&gt;"/&gt;
                                    &lt;input type="submit" value="upload" /&gt;
                                    &lt;!--&lt;input type="text" name="icon" class="tb" title="&lt;?=lang ('icon');?&gt;" /&gt;--&gt;



function post() {
            $c = "";
            $data['action'] = $this->input->post('action');


else if ($data['action']=='save') {
                $item = $this->input->post("item");
                $dt['module_name'] = $this->input->post('module_name');
                $report_exist = $this->Reports->getDetailsByName($dt);

                if ($report_exist && $item != $report_exist['module_id'])
                    $c .= 'Report already exists.';
                    $data['action'] = 'exist';
                $config['upload_path'] = './reports/module/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size']    = '100';
                $config['max_width']  = '1024';
                $config['max_height']  = '768';
                $this->load->library('upload', $config);
                $file_data = $this->upload->data();
                $file_name = $file_data['file_name'];
                $fields = array(

                /*foreach ($fields as $field)
                    if (isset($_POST[$field])) $dt[$field] = $this->input->post($field);
                $item = $this->input->post("item");
                $rows = $this->input->post("row0");
                $data['item'] = $this->Reports->save($fields,$item,$rows);    

                $c .= 'Successfully saved item';

            $data['response'] = $c;
            $json['json'] = $data;


function save($data,$item,$details) {
        if ($item == '') {
            $data['link'] = "reports/view";
            $this->db->insert('sec_reports', $data);
            $item = $this->db->insert_id();
        else {
            $this->db->where('module_id', $item);
            $this->db->update('sec_reports', $data);
        return $item;

i got only other fields to be save in database..only the icon field does'nt save in database...the value always saved is empty or null...

please help me to find the problem in my code..

tnx in advance..

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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