Welcome Guest, Not a member yet? Register   Sign In
Upload Class
#1

[eluser]ShoeLace1291[/eluser]
I've gone over the article in the user guide on the file uploading class, but I just can't seem to figure out what I'm doing wrong. I'm combining both the db class and the upload class so users can upload a photo. I use the db class to later get the photos by the user id, etc. When I submit the form, it tells me that I did not select a file to upload. The database function executes properly, but the file itself doesn't upload.

Here is my controller:
Code:
<?php

class Photos extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->library('gallery/photo');
        $this->load->library('gallery/album');
    }
    
    function upload(){
    
        $member = $this->member->user_context();
        
        $this->load->library('form_validation');
    
        $this->template->overall_header($title = 'Upload Photo');
        
        $this->form_validation->set_rules('album', 'Album', 'required');
        $this->form_validation->set_rules('title', 'Title', 'required|min_length[5]|max_length[30]|xss_clean');
        $this->form_validation->set_rules('caption', 'Caption', 'required|min_length[5]|max_length[150]|xss_clean');
        if($this->form_validation->run() == FALSE){
        
        $data = array(
                'FORM_OPEN' => form_open_multipart('gallery/photos/upload/'),
                'VALIDATION_ERRORS' => validation_errors(),
                'MEMBER_ALBUMS' => $this->album->dropdown($member['id'], $this->uri->segment(4))
                );
        
        $this->parser->parse('gallery/photo_upload.tpl', $data);
        
        } else {
        
            $info = array(
                'album_id' => $this->input->post('album'),
                'title' => $this->input->post('title'),
                'caption' => $this->input->post('caption'),
                'author_id' => $member['id']
                );
            if($this->photo->upload_db($info)){
    
                $config = array(
                    'upload_path' => 'attachments',
                    'allowed_types' => 'jpg',
                    'max_size' => 100,
                    'max_width' => 750,
                    'max_height' => 750,
                    'file_name' => $this->photo->insert_id.".jpg"
                    );
                
                $this->load->library('upload', $config);
                if($this->upload->do_upload()){
                    redirect('gallery/photos/'.url_title($this->input->post('title')).'/'.$this->photo->insert_id);
                } else {
                    $data = array(
                            'ERROR_TITLE' => 'File Upload Error',
                            'ERROR_MESSAGE' => $this->upload->display_errors(),
                            'ERROR_TYPE' => 'bad'
                            );
                    $this->parser->parse('body_error.tpl', $data);
                }
            } else {
                $data = array(
                            'ERROR_TITLE' => 'System Error',
                            'ERROR_MESSAGE' => 'The system encountered an error while updating the database with the necessary information.',
                            'ERROR_TYPE' => 'ok'
                            );
                $this->parser->parse('body_error.tpl', $data);
            }
        }
        
    }
    
}

Here is the form:
Code:
<div class="container">
            <div id="content-left">
                <div id="post" style='clear: both;'>
                    <h2>Upload Photo</h2>
                    <div class="half-last">
                        <div class="purelist arrows">
                            <ul>
                                {VALIDATION_ERRORS}
                            </ul>
                        </div>
                    </div>

                    {FORM_OPEN}
                           <p>
                            <label for="album">Album:</label>
                            {MEMBER_ALBUMS}
                        </p>
                        <p>
                            <label for="title">Photo Title: </label>
                            &lt;input id="title" name="title" /&gt;
                        </p>        
                        <p>
                            <label for="caption">Caption:</label>
                            &lt;input id='caption' name='caption' /&gt;
                        </p>                      
                        <p>
                                          
                           <p>
                            <label for="file">Upload File:</label>
                            &lt;input id='file' name='file' type='file' /&gt;
                        </p>
                        
                          
                        <p>

                            &lt;input type="submit"  value="Submit" id="submit-btn"/&gt;
                        </p>
                        <div id="info"></div>
                        &lt;/form&gt;
                </div>
            </div>
        </div>
#2

[eluser]jgetner[/eluser]
are you getting an error message?
#3

[eluser]ShoeLace1291[/eluser]
Yes, "You did not select a file to upload"
#4

[eluser]InsiteFX[/eluser]
Code:
$config = array(
    'upload_path' => 'attachments',
    'allowed_types' => 'jpg',
    'max_size' => 100,
    'max_width' => 750,
    'max_height' => 750,
    'file_name' => $this->photo->insert_id.".jpg"
);

echo var_dump($config);

InsiteFX
#5

[eluser]ShoeLace1291[/eluser]
Quote:array
'upload_path' => string 'attachments' (length=11)
'allowed_types' => string 'jpg' (length=3)
'max_size' => string '100' (length=3)
'max_width' => string '750' (length=3)
'max_height' => string '750' (length=3)
'file_name' => string '15.jpg' (length=6)
#6

[eluser]ShoeLace1291[/eluser]
Any ideas?
#7

[eluser]InsiteFX[/eluser]
Your missing your do_upload method!

Read the CodeIgniter User Guide on the File Uploading Class.

InsiteFX
#8

[eluser]ShoeLace1291[/eluser]
no, it's there. It's nested in an if statement determining whether or not the file upload was successful or not.
#9

[eluser]khsmith[/eluser]
You have to specify the &lt;input type="file" ...&gt; element name attribute in the "do_upload()" function call, otherwise it defaults to 'userfile' your form is set to 'file' for the upload HTML form element.

Hope this helps.

Best,
K. H. Smith




Theme © iAndrew 2016 - Forum software by © MyBB