Welcome Guest, Not a member yet? Register   Sign In
file upload
#1

[eluser]bramb79[/eluser]
Hi,

I'm trying to use the upload library included in CI. It seems there is a problem with my system's configuration though: when my own script didn't work, I tried to copy the example from the User Guide to see if that would work. There just doesn't seem to be anything stored in $this->upload->data()...

Are there any settings that I might have overlooked? Could there be another problem?

thanks!
#2

[eluser]gtech[/eluser]
one thing that caught me out when I first used upload is that the form has to have a field called "userfile" which is used to store the path of the file to upload. if this is not in the post data the do_upload function will not work.

also ensure you have created the uploads directory, at the root of your install

[edit]
If all else fails you can look at the upload library in system\libraries\Upload.php and look at the do_upload function.. you could put some debug (either log_debug or echo) just to see how far it is getting. I tend to but debug in the codeigniter code if I am not sure how a function is working (not forgetting to make a backup copy).
[/edit]
#3

[eluser]bramb79[/eluser]
Thanks for your answer. I copied the example from the user guide, there is a 'userfile' field in my form. I am not even uploading the file actually. I seems like there is no data passed at all, because the $this->upload->data() is not filled...

This is my code:
Code:
<?php echo form_open_multipart('upload/do_upload'); ?>
<input type="file" name="userfile" size="20" />
<br /><br />
&lt;input type="submit" value="upload" /&gt;
&lt;/form&gt;

Code:
function do_upload()
{
    $config['upload_path'] = './uploads/';
    
    $this->load->library('upload', $config);
        
    print_r($this->upload->data());
}
#4

[eluser]gtech[/eluser]
ok you need to make a call to $this->upload->do_upload() otherwise it will not even attempt to upload the file.

look at the upload library in system\libraries\Upload.php and the do_upload function.. you will see that is the function that uploads the file and sets the data.


try using the documentions upload controller without cutting corners
Code:
&lt;?php

class Upload extends Controller {
    
    function Upload()
    {
        parent::Controller();
        $this->load->helper(array('form', 'url'));
    }
    
    function index()
    {    
        $this->load->view('upload_form', array('error' => ' ' ));
    }

    function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        
        $this->load->library('upload', $config);
    
                // this line is important.
        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->view('upload_success', $data);
        }
    }    
}
?&gt;
#5

[eluser]bramb79[/eluser]
I commented that out, because I wanted to test if there was any data at all. Still when I add it again it gives the following error:

'The upload path does not appear to be valid.'

I think this might be because some of the elements in the data array (like filename, etc) are empty?
#6

[eluser]bramb79[/eluser]
Hmm ok.. It seems to work now I have actually created the ./uploads folder. Strange but true Smile

Thanks!
#7

[eluser]gtech[/eluser]
yep the line you commented out is the function that gets the file from the clients computer and sets the data variable.. so thats why you had no data Smile. If you don't create the uploads folder the server has nowhere to place the uploaded file, and hence the error you were seeing.

anyway glad you figured it all out, I had some fun and games with the upload when I first used it.




Theme © iAndrew 2016 - Forum software by © MyBB