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

#11
[eluser]überfuzz[/eluser]
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.

#12
[eluser]wowdezign[/eluser]
Well, I tried
Code:
if(!empty($_FILES)){
  // Do upload
}

and it still errors out.

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

#13
[eluser]überfuzz[/eluser]
[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..?

Code:
if(!empty($_FILES))
{
   //put the code that's doing the upload here
}

#14
[eluser]wowdezign[/eluser]
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:
Code:
function do_upload($field = 'userfile'){
    // Is $_FILES[$field] set? If not, no reason to continue.
    if ( ! isset($_FILES[$field])){
        $this->set_error('upload_no_file_selected');
        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'];
        switch($error){
            case 1:    // UPLOAD_ERR_INI_SIZE
                $this->set_error('upload_file_exceeds_limit');
                break;
            case 2: // UPLOAD_ERR_FORM_SIZE
                $this->set_error('upload_file_exceeds_form_limit');
                break;
            case 3: // UPLOAD_ERR_PARTIAL
               $this->set_error('upload_file_partial');
                break;
            case 4: // UPLOAD_ERR_NO_FILE
               $this->set_error('upload_no_file_selected');
                break;
            case 6: // UPLOAD_ERR_NO_TMP_DIR
                $this->set_error('upload_no_temp_directory');
                break;
            case 7: // UPLOAD_ERR_CANT_WRITE
                $this->set_error('upload_unable_to_write_file');
                break;
            case 8: // UPLOAD_ERR_EXTENSION
                $this->set_error('upload_stopped_by_extension');
                break;
            default :   $this->set_error('upload_no_file_selected');
                break;
        }
        return FALSE;
    }
When I submitted my form with an empty upload field I got the following for $_FILES:
Code:
Array
(
    [userfile] => Array
        (
            [name] =>
            [type] =>
            [tmp_name] =>
            [error] => 4
            [size] => 0
        )
)
So the solution for me was to do:
Code:
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:.

#15
[eluser]wowdezign[/eluser]
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:

#16
[eluser]überfuzz[/eluser]
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?

#17
[eluser]wowdezign[/eluser]
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. :-)

#18
[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.

jeremy

#19
[eluser]Genjo[/eluser]
my solution is:
Code:
if($_FILES['news_img']['name'] != NULL)
{
   //do your upload code
}

It's ok for me, tested.

#20
[eluser]cestaz18[/eluser]
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

VIEW

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;/form&gt;
                                    &lt;!--&lt;input type="text" name="icon" class="tb" title="&lt;?=lang ('icon');?&gt;" /&gt;--&gt;
                                </div>


CONTROLLER

IN MY FUNCTION POST..HERE'S MY FILE UPLOADING CODE

Code:
function post() {
            //$this->output->enable_profiler(true);
            $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';
                }
                else
                {
                
                
                $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);
                
                
              
            
                
                $this->upload->do_upload();
                
                $file_data = $this->upload->data();
                $file_name = $file_data['file_name'];
                
            
            
                            
                $fields = array(
                "module_name"=>$this->input->post('module_name'),
                "slug"=>$this->input->post('slug'),    
                 "icon"=>$file_data['file_name'],      
                "report_file_name"=>$this->input->post('report_file_name'),
                "sql_select"=>$this->input->post('sql_select'),
                "sql_filter"=>$this->input->post('sql_filter'),
                "sql_order"=>$this->input->post('sql_order')
                );        

                /*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;
                
            }

MODEL

Code:
function save($data,$item,$details) {
        if ($item == '') {
            $data['link'] = "reports/view";
            
            $this->db->trans_start();
            $this->db->insert('sec_reports', $data);
            $item = $this->db->insert_id();
            $this->save_detail($item,$details);            
            $this->db->trans_complete();
            }
        else {
            $this->db->trans_start();
            $this->db->where('module_id', $item);
            $this->db->update('sec_reports', $data);
            $this->save_detail($item,$details);                
            $this->db->trans_complete();
        }
        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.