Welcome Guest, Not a member yet? Register   Sign In
file upload fails but POST data looks fine (SOLVED - bug?)
#1

[eluser]munkeh[/eluser]
I'm trying to upload a file and while the form seems to be working the upload helper gives me an error: You did not select a file to upload.

The POST data is fine:
$_POST['upload_file'] test.jpg
$_POST['file_notes'] test

permissions on the /var/www/website/htdocs/system/uploads directory are fine. PHP config as follows:
file_uploads on
max_file_uploads 20
upload_max_filesize 2M

What am I doing wrong?

My view:
Code:
<?php echo form_fieldset('upload'); ?>
        <?php echo form_open_multipart('add_claim/upload_file', 'name="upload_form"');?>
            <ul>
                <li>
                    &lt;?php echo form_upload("upload_file"); ?&gt;
                </li>
                <li>
                    &lt;?php echo form_label('File Notes', 'file_notes');?&gt;
                    &lt;?php echo form_input($form_file_notes); ?&gt;
                </li>
            </ul>

            &lt;input type="submit" value="upload"/&gt;

        &lt;?php echo form_close(); ?&gt;                
    &lt;?php echo form_fieldset_close(); ?&gt;

My Controller:
Code:
function upload_file() {
        
    $this->form_validation->set_rules('file_notes','lang:file_notes','trim|max_length[256]');
    $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
    
    if ($this->form_validation->run() == FALSE)
    {
        $form_file_notes = array(
            'name'        => 'file_notes',
            'id'          => 'file_notes',
            'value'       => set_value('file_notes'),
            'maxlength'   => '256'
        );
        
        $data['form_file_notes'] = $form_file_notes;    

        $data['main_content'] = 'claim/upload_file';
        $this->load->view('includes/template', $data);            
    }
    else
    {
        $config['upload_path'] = './uploads/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['encrypt_name'] = 'true';
                $config['remove_spaces'] = 'true';
        
        $this->load->library('upload', $config);            
    
        if (!$this->upload->do_upload('upload_file'))
        {
            print_r($this->upload->display_errors());
        }    
        else
        {
            print_r($this->upload->data());
        }            
    }

}
#2

[eluser]munkeh[/eluser]
Solved the problem but possibly found a bug/feature? Or maybe I was just doing things wrong all along.

I have been creating forms like this throughout my applications:
Code:
&lt;?php echo form_open('add_claim/upload_file', 'name="upload_form"'); ?&gt;
This results in the following HTML (http:// removed to get around the spam filter):
Code:
&lt;form action="website/add_claim/upload_file" name="upload_form" method="post"&gt;

According to the docs I'm supposed to be passing the attributes as an array but juat adding them to the form_open function seemed to be working just fine so I kept on doing it. Truth be told I only really glanced at the docs for the form helper, so my bad there.

Anyway, turns out if you do the same thing with a multipart form it doesn't work:
Code:
&lt;?php echo form_open_multipart('add_claim/upload_file', 'name="upload_form"');?&gt;
gives the following HTML:
Code:
&lt;form action="website/add_claim/upload_file" mame="upload_form" method="post"&gt;

note: mame="upload_form" instead of name="upload_form". This, I guess, was causing the do_upload() function to fail.

Anyway, figured I'd at least update the thread in case anyone else was stumped by the same thing.




Theme © iAndrew 2016 - Forum software by © MyBB