• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File upload validation

#11
[eluser]mindesign[/eluser]
View:
Code:
<?php
if($this->session->userdata("UserID")){
    //echo flash data of result
    echo $this->session->flashdata("PhotoAddConfirmation");
    //text validation error
    echo validation_errors();
    
    $gallery_dropdown = array();
    $gallery_dropdown[0]="Unsorted";
    if($galleries !=NULL){
        foreach($galleries as $gallery){
            $gallery_dropdown[$gallery->GalleryID] = $gallery->GalleryName;
        }
    }

    echo form_open_multipart('gallery/addPhoto');
        echo form_fieldset('Add Photo');
            echo "<div class='field'>";
                echo form_label('Title:','title');
                echo form_input(array("name"=>"title","value"=>set_value('title'),"class"=>"text"));
            echo "</div>";
            echo "<div class='field'>";
                echo form_label('Gallery:','gallery');
                echo form_dropdown('gallery',$gallery_dropdown);
            echo "</div>";
            echo "<div class='field'>";
                echo form_label('Image:','image');
                echo form_upload('image');
            echo "</div>";
            echo "<div class='buttons'>";
                echo form_submit(array("name"=>"submit","value"=>"Upload Photo","class"=>"button"));
            echo "</div>";
        echo form_fieldset_close();
    echo form_close();
    
}

Controller:
Code:
&lt;?php
class Gallery extends Controller{
    function Gallery(){
        parent::Controller();
        $this->load->library('form_validation');
        $this->load->model('gallery_mdl');
    }
    
    function index(){
        //display gallery view of all theediffrent galleries
        
    }
    
    function addPhoto(){
        //upload new photo
        if($this->session->userdata("UserID")){    
            if($this->gallery_mdl->getGalleries())
                $data['galleries']=$this->gallery_mdl->getGalleries();
            else
                $data['galleries']=NULL;
                
            $this->form_validation->set_rules('title','Title','trim|max_length[20]|xss_clean');
            $this->form_validation->set_rules('image', 'Image', 'required|callback__do_upload');
            $this->form_validation->set_rules('gallery','Gallery', 'required');
                        
            if($this->form_validation->run()){
            
            }
            
            
            $this->template->write('title',"Add Photo | Dalnavert Kennels");
            $this->template->write_view('content',"admin/add_photo", $data);
            $this->template->render();
        }
        else{
            $this->template->write('title',"Access Denied");
            $this->template->write_view('content',"Access_Denied");
            $this->template->render();
        }
    }
    
    function addGallery(){
        //Add Gallery to Gallery list
        if($this->session->userdata("UserID")){
            if($this->gallery_mdl->getGalleries())
                $data['galleries']=$this->gallery_mdl->getGalleries();
            else
                $data['galleries']=NULL;
            
            
            $rules['gallery'] = 'trim|required|xss_clean|max_length[100]';
            $this->validation->set_rules($rules);
            $fields['gallery'] = 'Gallery';
            $this->validation->set_fields($fields);
            $this->validation->set_error_delimiters('<li>', '</li>');
            if($this->validation->run()){
                $insert_gallery = $this->gallery_mdl->addGallery($this->validation->gallery);
                if($insert_gallery){
                    $this->session->set_flashdata("GalleryAddConfirmation", $this->validation->gallery." has been added to the list of galleries.");
                    redirect('gallery/addGallery');
                }
                else{
                    $this->session->set_flashdata("GalleryAddConfirmation", $this->validation->gallery." has not ben added. An Error occurred upon trying to insert it into the database. Contact Administrator.");
                    redirect('gallery/addGallery');
                }
                
            }
            $this->template->write('title',"Add Gallery | Dalnavert Kennels");
            $this->template->write_view('content',"admin/add_gallery", $data);
            $this->template->render();
        }
        else{
            $this->template->write('title',"Access Denied");
            $this->template->write_view('content',"Access_Denied");
            $this->template->render();
        }
    }
    
    /*-----------------Callback Functions--------------------*/
    function _do_upload($file)
    {
        $config['upload_path'] = './public/images/temp_images/';
        $config['allowed_types'] = 'jpg|jpeg|png|gif';
        $this->load->library('upload', $config);
            
        if ( ! $this->upload->do_upload('image'))
        {
            // set the validation error using the errors provided by the upload class
            $this->form_validation->set_message('_do_upload', $this->upload->display_errors());
            return FALSE;
        }    
        else
        {
            return TRUE;
        }
    }
}

File upload in view is called image.

in my controller , I haven't done anything past running the validation as I want to get that working first.

thanks!

#12
[eluser]mindesign[/eluser]
Ok. seems like I have it working now. not sure what changed but it works like i've shown above.

One other question though. I want to now resize and move the photo that was uploaded.

How do I get $this->upload->data(); back from the callback function?

thanks!

grant

#13
[eluser]pistolPete[/eluser]
Either you could apply the resize/move action in the callback, otherwise I'd use a class variable to store that data.

#14
[eluser]mindesign[/eluser]
cool. will try that out! thanks for the help!

#15
[eluser]mindesign[/eluser]
This might be a dumb question but how to i set it to a class variable. I thought I knew how but it just keep getting unexpected variable errors. any help?

thanks!

grant

#16
[eluser]pistolPete[/eluser]
PHP4:
Code:
&lt;?php
class Gallery extends Controller{
    var $data;
    function Gallery()
    {
        // initialise the class variable
          $this->data = array();
        ...
    }
    function _do_upload($file)
    {
        ...
          $this->data = $this->upload->data();
    }  
}

PHP5:
Code:
&lt;?php
class Gallery extends Controller{
    private $data;
...

Have a look at php.net.

#17
[eluser]mindesign[/eluser]
ahh forgot $this->. thanks

#18
[eluser]Bikun[/eluser]
Do I understand correctly that if file is not mandatory, then you can't just use callback? The problem is that I have mandatory fields in the form and file is not mandatory, but I need to check that file is valid for upload. If it's not valid, then I need to refill all field values (I do it with validation class) and show notification that file is not correct.

Is there a simple way of doing that?

#19
[eluser]mindesign[/eluser]
Yup. when I put all this in the callback, if the file is not the right type, it returns the error like normal. If you do not want the file upload to be manditory, in the callback, have an if statement checking if a file was selected if now, skip and return true, if not, run the callback like normal.

#20
[eluser]Bikun[/eluser]
Hmm, really, that should work Smile Spring, no vitamins, head doesn't work Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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