CodeIgniter Forums

Full Version: Upload File Problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]codeign00b[/eluser]
Hi codeignitr experts.

I need help with uploading files with codeigniter. My problem is that I can't upload anything, everytime I get You did not select a file to upload.

Here is the function in my controller:

Code:
function upload()
    {
        //loading libraries
        $data['message'] = '';
        $this->load->library( array('form_validation', 'email') );
        $this->load->helper( array('string', 'array', 'date') );
        $this->load->helper('form');
        $config['upload_path'] = './files/';
        $config['allowed_types'] = 'jpg';
        $config['max_size'] = '5000';
        $this->load->library('upload', $config);
      
        //form validation
        $this->form_validation->set_rules('userfullname', 'User Full Name', 'matches[userfullname]|xss_clean');
        $this->form_validation->set_rules('filename', 'File', 'trim');
        if ($this->form_validation->run() == FALSE)
        {
            //page has not run or validation error
            $data['main_content'] = 'view_files_upload';
            $this->load->view('view_main', $data);
        }
        else
        {      
            //get userid
            $this->load->model('Model_files');
            $userfullname = $this->input->post('userfullname');
            $userid = $this->Model_files->get_userid($userfullname);

            //get file date by Y-M-D for filtering
            $reportyear = date("Y");
            $reportmonth = date("n");
            $reportday = date("d");

            if (!$this->upload->do_upload())
            {
              $error = array('error' => $this->upload->display_errors());
                print_r($error);
            }


            else
            {

        $this->upload->do_upload();
                $data = array('upload_data' => $this->upload->data());

                $filename = $data['file_name'];
                
                $filestable= array(
                           'userid'=> $userid['userid'],
               'filename'=>$filename,
                           'fileowner'=>$userfullname,
                 'fileyear'=>$fileyear,
                           'filemonth'=>$filemonth,
                           'fileday'=>$fileday
                             );
                $this->Model_files->upload_files($filestable);
                redirect('files/upload');
            }
        }        
    }

I have note that I have server settings like this:

/home
|
+ app
|
+ application
|
+ system
|
+ files
|
+ scripts
|
+ index.php

index.php is symlinked to www folder

View file (form):

<?php
Code:
...
$submit = 'class="submit"';
$file = array(
              'name'        => 'filename',
              'id'          => 'filename',
              'value'       => (set_value('filename')),
              'type'       => 'file',

            );
echo '<div id="form-container">';
echo form_open_multipart('files/upload');
echo '<label for="userfullname" id="labeluserfullname">User Full Name</label>';
echo form_input('userfullname', set_value('userfullname'));
echo '<br></br>';
echo '<label for="filename" id="labelfile">Select File</label>';
echo form_input($file);
echo '<br></br>';
echo form_submit('submit', 'Upload', $submit);  
echo form_close();
echo '</div>';
?&gt;
...

Please help me with.

Than you all!

p.s. ignore error pages, they are set to view the errors when developing.

El Forum

[eluser]codeign00b[/eluser]
Anyone?

El Forum

[eluser]fesweb[/eluser]
i didn't read your code line-for-line, but you are not telling the library what file field you want to upload...

So, in your case:
Code:
$this->upload->do_upload('filename');

El Forum

[eluser]codeign00b[/eluser]
I've changed like you said, but I'm getting the same error:

You did not select a file to upload.

Do I have the wrong path?

El Forum

[eluser]fesweb[/eluser]
It's always worth looking at the path you are trying to write to. Echo it to make sure that it is what you think it is, and then check permissions to make sure that Apache/PHP can write to that directory.

Also, double check your actual form's file field and your do_upload('your_file_field_name') to make sure that those two field names are in agreement.

El Forum

[eluser]codeign00b[/eluser]
Folder permissions are at 777 of course.

Echo gives me /var/www/admin/files

I have the folder there called files. So it's not the path.

Any other ideas why this doesnt work?

p.s. they match. id and name of the upload field is file and I have do_upload('file').

p.s.s. Now I'm getting this error:

The upload destination folder does not appear to be writable. although the folder permisions are st to 777, so it's writable as it can be.

El Forum

[eluser]LuckyFella73[/eluser]
I didn't test this but you could try
Code:
// in the validation part of your code:

// this one doesn't make sense
// the rules or only for form elements which are in the $_POST array
// your file is send in the $_FILE array
$this->form_validation->set_rules('filename', 'File', 'trim');// comment out this line



// later on this part should look like:

// ... other code here

if (!$this->upload->do_upload('filename'))
{
    echo $this->upload->display_errors();
}
else
{
    // $this->upload->do_upload(); // you don't need that one
    
    $data = $this->upload->data();
    
    $filename = $data['file_name'];
    
    $filestable= array(
        'userid' => $userid['userid'],
        'filename' => $filename,
        'fileowner' => $userfullname,
        'fileyear' => $fileyear,
        'filemonth'=>$filemonth,
        'fileday'=>$fileday
    );
    
    $this->Model_files->upload_files($filestable);
    redirect('files/upload');
}

Hope that helps

El Forum

[eluser]codeign00b[/eluser]
Thank you! Now everything works like a charm.

Thank you all for your help.