Welcome Guest, Not a member yet? Register   Sign In
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!


Messages In This Thread
File upload validation - by El Forum - 02-23-2009, 05:24 PM
File upload validation - by El Forum - 02-23-2009, 05:37 PM
File upload validation - by El Forum - 02-24-2009, 06:36 PM
File upload validation - by El Forum - 02-24-2009, 08:20 PM
File upload validation - by El Forum - 02-25-2009, 05:18 AM
File upload validation - by El Forum - 02-25-2009, 05:36 AM
File upload validation - by El Forum - 02-25-2009, 05:40 AM
File upload validation - by El Forum - 02-25-2009, 06:14 PM
File upload validation - by El Forum - 03-22-2009, 11:40 AM
File upload validation - by El Forum - 03-22-2009, 12:23 PM
File upload validation - by El Forum - 03-22-2009, 12:29 PM
File upload validation - by El Forum - 03-22-2009, 03:33 PM
File upload validation - by El Forum - 03-22-2009, 04:07 PM
File upload validation - by El Forum - 03-22-2009, 04:31 PM
File upload validation - by El Forum - 03-22-2009, 04:52 PM
File upload validation - by El Forum - 03-22-2009, 05:00 PM
File upload validation - by El Forum - 03-22-2009, 05:06 PM
File upload validation - by El Forum - 03-28-2009, 09:32 AM
File upload validation - by El Forum - 03-28-2009, 09:37 AM
File upload validation - by El Forum - 03-28-2009, 10:50 AM
File upload validation - by El Forum - 04-11-2009, 11:31 AM
File upload validation - by El Forum - 04-11-2009, 11:44 AM
File upload validation - by El Forum - 04-11-2009, 12:09 PM
File upload validation - by El Forum - 04-11-2009, 12:19 PM
File upload validation - by El Forum - 04-11-2009, 12:33 PM
File upload validation - by El Forum - 04-11-2009, 05:39 PM
File upload validation - by El Forum - 04-12-2009, 02:41 AM
File upload validation - by El Forum - 04-12-2009, 05:08 AM
File upload validation - by El Forum - 04-12-2009, 08:17 AM
File upload validation - by El Forum - 04-12-2009, 10:50 AM
File upload validation - by El Forum - 10-23-2009, 10:09 AM
File upload validation - by El Forum - 10-23-2009, 10:44 AM
File upload validation - by El Forum - 12-23-2010, 10:56 PM
File upload validation - by El Forum - 02-27-2011, 11:10 AM



Theme © iAndrew 2016 - Forum software by © MyBB