Welcome Guest, Not a member yet? Register   Sign In
Image upload and return function problem
#1

[eluser]kitefr[/eluser]
Hi guys,

I'm just wondering if i can use the "return" word inside an upload function.

like this one :

Code:
public function do_upload($field_name)
    {
        $this->load->helper('image_manipulation');
        
        $config = array(
            'allowed_types' => 'jpg|jpeg|png',
            'upload_path' => $this->series_path,
            'max_size' => 1000,
            'overwrite' => TRUE,
            'max_width' => '0'
        );
        $this->load->library('upload', $config);
        
        if( $this->upload->do_upload($field_name) ) {
            
            $uploaded_data = $this->upload->data();
            
            $file_name = $uploaded_data['file_name'];
            
            if($uploaded_data['image_width'] > '715' OR $uploaded_data['image_height'] > '500')
            {
                $config['image_library'] = 'gd2';
                $config['source_image'] = $this->series_path.'\\'.$file_name;
                $config['create_thumb'] = FALSE;
                $config['maintain_ratio'] = TRUE;
                $config['width'] = 715;
                $config['height'] = 500;
                
                $this->load->library('image_lib', $config);
                $this->image_lib->resize();
            }
            
            $safe_name = underscore($this->input->post('title'), 'underscore', TRUE).'_'.$this->input->post('typeserie').strtolower($uploaded_data['file_ext']);
            if(file_exists($this->series_path.'\\'.$safe_name))
            {
                unlink($this->series_path.'\\'.$safe_name);
            }
            $file_name = rename($this->series_path.'\\'.$file_name, $this->series_path.'\\'.$safe_name);
            
            $uploaded_data['news_filename'] = $safe_name;
            return $uploaded_data;
        }
    }

The function works great but the return doesn't return anything :/
So i use this function in a form_validation run process. It work great except the array of data is not returned so i can't add the file name to my data array to pass it to a model for sql creation.
#2

[eluser]kitefr[/eluser]
Here is my controller view (cut because too large):

Code:
function add()
    {
        (...)

        //Start the validation routines...
        if($this->form_validation->run()) {
            
            if($this->members->check_user_level(1)){
                $temp_data['confirmed'] = $this->input->post('confirmed');
            } else {
                $temp_data['confirmed'] = 0;
            }
            
            if($this->input->post('typeserie') == 'film')
            {
                $temp_data['nb_ep'] = '0';
                $temp_data['nb_sp'] = '0';
                $temp_data['link_wiki_fr'] = $this->input->post('link_wiki_fr');
                $temp_data['link_wiki_us'] = $this->input->post('link_wiki_us');
            }
            
            if($this->input->post('typeserie') == 'drama')
            {
                $temp_data['nb_ep'] = $this->input->post('nb_eps');
                $temp_data['nb_sp'] = $this->input->post('nb_spe');
                $temp_data['link_wiki_fr'] = $this->input->post('link_wiki_fr');
                $temp_data['link_wiki_us'] = $this->input->post('link_wiki_us');
            }
            
            if($this->input->post('typeserie') == 'anime')
            {
                $temp_data['nb_ep'] = $this->input->post('nb_eps');
                $temp_data['nb_sp'] = $this->input->post('nb_spe');
                $temp_data['link_wiki_fr'] = '';
                $temp_data['link_wiki_us'] = '';
            }
            
            //Taking care of the uploaded file...
            //Call the model function for uploading
            if(image_check('affiche_upload') == TRUE) {
                $this->series_model->do_upload('affiche_upload', TRUE);
            }
            
            //change the string of genre to array and serilize it !
            if($this->input->post('genre'))
            {
                $genre = serialize_genre($this->input->post('genre'));
            }
            
            //change the summary sources infos to an array and serialize it !
            if($this->input->post('summary_source_title'))
            {
                if(!$this->input->post('summary_source_link'))
                {
                    return NULL;
                } else {
                    $source = array(
                        $this->input->post('summary_source_title') => $this->input->post('summary_source_link'),
                    );
                    $source = serialize($source);
                }
                
            }
            
            $post_data = array(
                'user_id'          => $this->input->post('user_id'),
                'title_original'   => $this->input->post('original_title'),
                'title'            => $this->input->post('title'),
                'title_other'      => $this->input->post('other_title'),
                'typeserie'        => $this->input->post('typeserie'),
                'date_release'     => $this->input->post('date_release'),
                'nb_episodes'      => $temp_data['nb_ep'],
                'nb_speciaux'      => $temp_data['nb_sp'],
                'affiche'          => $uploaded_data['new_filename'],
                'country'          => $this->input->post('pays'),
                'genre'            => $genre,
                'summary'          => $this->input->post('summary'),
                'summary_source'   => $source,
                'link_official'    => $this->input->post('link_official'),
                'link_wiki_fr'     => $temp_data['link_wiki_fr'],
                'link_wiki_us'     => $temp_data['link_wiki_us'],
                'confirmed'        => $temp_data['confirmed'],
            );
            
            $this->series_model->create_serie($post_data);
  
(...)

I hope some of ou will be able to help me, if you need more code, just ask !

Thanks,
Kitefr
#3

[eluser]Unknown[/eluser]
Whasup man!
You said the return has nothing. The system is doing the upload of file or not?
#4

[eluser]kitefr[/eluser]
Hi Praxedes !

The uploading of the image file, the rename and the resize works perfectly.
It's just when the function ends the return doesn't return anything for i don't have the new_name variable for add it into my post array for mysql.

Otherwise, all the rest works perfectly fine !
#5

[eluser]toopay[/eluser]
This maybe just simple solution. If you use session library, try to store your $safe_name in session userdata, then pass it to your array. if you describe your flow architecture better, maybe i can provide better solution.
#6

[eluser]toopay[/eluser]
in your model, at upload function,
Code:
$uploaded_data['news_filename'] = $safe_name;
return $uploaded_data;

change to
Code:
$uploaded_data['news_filename'] = $safe_name;
$this->session->set_userdata('tmpimgfilename',$safe_name);
return $uploaded_data;

now, you can pass it to your controller like this
Code:
$post_data = array(
                'user_id'          => $this->input->post('user_id'),
                'title_original'   => $this->input->post('original_title'),
                'title'            => $this->input->post('title'),
                'title_other'      => $this->input->post('other_title'),
                'typeserie'        => $this->input->post('typeserie'),
                'date_release'     => $this->input->post('date_release'),
                'nb_episodes'      => $temp_data['nb_ep'],
                'nb_speciaux'      => $temp_data['nb_sp'],
                //'affiche'          => $uploaded_data['new_filename'],
                'affiche'          =>$this->session->userdata('tmpimgfilename')
                'country'          => $this->input->post('pays'),
                'genre'            => $genre,
                'summary'          => $this->input->post('summary'),
                'summary_source'   => $source,
                'link_official'    => $this->input->post('link_official'),
                'link_wiki_fr'     => $temp_data['link_wiki_fr'],
                'link_wiki_us'     => $temp_data['link_wiki_us'],
                'confirmed'        => $temp_data['confirmed'],
            );
like i said, this just a simple solution, because I dont know your flow process as well.
#7

[eluser]kitefr[/eluser]
Thanks toopay !

I will actually use the session, much easy.
Well, my flow process is pretty hard to understand specially in this case.

it works well with the session.

i just change :
Code:
'affiche'          => $this->session->userdata('tmpimgfilename'),




Theme © iAndrew 2016 - Forum software by © MyBB