• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File Upload class

#1
[eluser]E303[/eluser]
I can't seem to get the File Upload to work..

This is my model
Code:
function exhibitionsAddDB()
    {
        
        //insert into db
        $picture = $_FILES['userfile'];
        $url = url_title($_POST['title']);
        
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $this->upload->initialize($config);
        
        $file = $this->upload->do_upload($picture);
        $file_data = array('upload_data' => $this->upload->data());
        #$startDate = human_to_unix($_POST['date']);
        #$endDate = human_to_unix($_POST['enddate']);
        $data = array(
                        'title'            => $_POST['title'],
                        'description'    => $_POST['body'],
                        'date'            => $_POST['date'],
                        'endDate'        => $_POST['enddate'],
                        'artistId'        => $_POST['artist'],                
                        'url'            => $url,
                        'picture'        => $file_data['full_name'],     #this is line 224 I have also had this as $upload_data as well with no luck
                    );
        $this->db->insert('exhibitions', $data);            
    }

The error I get is
Quote:A PHP Error was encountered

Severity: Warning

Message: Illegal offset type in isset or empty

Filename: libraries/Upload.php

Line Number: 138

Then

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined index: full_name

Filename: admin/admin_model.php

Line Number: 224

#2
[eluser]gtech[/eluser]
You do not pass $picuture (in your case $_FILES['userfile']) to $this->upload->do_upload(); if userfile was c:\image.jpg in your code above do_upload would of looked for a post variable called c:\image.jpg... which of course is silly.

instead just do:

Code:
function exhibitionsAddDB()
    {
        
        // dont need this line
        // $picture = $_FILES['userfile'];
        $url = url_title($_POST['title']);
        
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $this->upload->initialize($config);
        
        // dont pass in the value of userfile, do_upload retrieves the value of
        // userfile by default if no parameters are passed in.
        // If you pass in a paramter it looks for that field name in the post data
        // instead of userfile.
        // ALSO do upload only returns true or false if it succeded or not, not the file!
        if ( ! $this->upload->do_upload())
        {
            $error = $this->upload->display_errors();
            echo $error;
            return;
        }    
        else
        {
            $file_data = array('upload_data' => $this->upload->data());
            $data = array(
                        'title'            => $_POST['title'],
                        'description'    => $_POST['body'],
                        'date'            => $_POST['date'],
                        'endDate'        => $_POST['enddate'],
                        'artistId'        => $_POST['artist'],                
                        'url'            => $url,
                        'picture'        => $file_data['full_path'],
                    );
            $this->db->insert('exhibitions', $data);  
        }            
    }

Also $file_data['full_name'] does not exist

you can either have 'full_path' or 'file_name' but not full_name.

#3
[eluser]E303[/eluser]
Thanks for that.. Now I just get on error
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined index: file_name

Filename: admin/admin_model.php

Line Number: 234
Code:
function exhibitionsAddDB()
    {
        
        // dont need this line
        // $picture = $_FILES['userfile'];
        $url = url_title($_POST['title']);
        
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $this->upload->initialize($config);
        
        // dont pass in the value of userfile, do_upload retrieves the value of
        // userfile by default if no parameters are passed in.
        // If you pass in a paramter it looks for that field name in the post data
        // instead of userfile.
        // ALSO do upload only returns true or false if it succeded or not, not the file!
        if ( ! $this->upload->do_upload())
        {
            $error = $this->upload->display_errors();
            echo $error;
            return;
        }    
        else
        {
            $file_data = array('upload_data' => $this->upload->data());
            $data = array(
                        'title'            => $_POST['title'],
                        'description'    => $_POST['body'],
                        'date'            => $_POST['date'],
                        'endDate'        => $_POST['enddate'],
                        'artistId'        => $_POST['artist'],                
                        'url'            => $url,
                        'picture'        => $file_data['file_name'],
                    );
            $this->db->insert('exhibitions', $data);  
        }            
    }

Also when I look in the uploads file I don't see the file so I guess it doesn't get uploaded?

#4
[eluser]gtech[/eluser]
sorry it should be
Code:
'picture'        => $file_data['upload_data']['file_name'],

or

Code:
'picture'        => $file_data['upload_data']['full_path'],

As if you look at the code above, it adds the results of upload->data() to an associative array with the key 'upload_data'

If your stuck don't be frightened to read the code in the libraries directory (upload.php) you can then see how the functions work.

have a search for 'function data' and 'function do_upload' for example, its all in php so relatively easy to follow.

#5
[eluser]E303[/eluser]
Thanks I think that is doing the trick.

However at the moment I have the script installed on a subdomain so the upload path isn't apparently valid. Do you have any clues as to how I should write it?

#6
[eluser]E303[/eluser]
Any idea on the path issue? The file still doesn't seem to get uploaded.

#7
[eluser]ontguy[/eluser]
is that directory writable?

you could use the file helper to check, it should look something like this:
Code:
$this->load->helper('file');
get_file_info('./uploads/', 'writable')

#8
[eluser]E303[/eluser]
Thanks it seems to be working now. I didn't actually do anything so, im not sure why it has just started working?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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