Welcome Guest, Not a member yet? Register   Sign In
Can I use the upload library inside a model?
#1

[eluser]Isern Palaus[/eluser]
Hello,

I want to use the upload library inside a model to write less in my controller functions. I've a lot of controllers that are add/edit and can upload a new file and write down the same code over and over I think that it's not the best option.

I've tried this (once my form is validated):
Code:
if(!empty($_FILES['imagen']['name']))
            {
                $upload = $this->imagenes_model->upload();
                
                if($upload['return'])
                    $this->imagenes_model->resize($upload['source_path'], $upload['file_name'], 'productos');
                else
                    log_message('error', 'ERROOOOOOOOOOOOOOR:'. $upload['message']);
            }

As you can see I use a log_message() which prints me: ERROOOOOOOOOOOOOOR:<p>You did not select a file to upload.</p> . So seems that the problem is passing the file to the upload function inside the model...

My imagenes_model:
Code:
&lt;?php
class Imagenes_model extends Model {
    
    private $_filename = NULL;
    private $_new_filename = NULL;
    private $_source_path = NULL;
    private $_upload_path = CREA_IMAGENES_UPLOAD_PATH;
    private $_width = CREA_IMAGENES_WIDTH;
    private $_height = CREA_IMAGENES_HEIGHT;
    private $_destination_path = CREA_IMAGENES_DESTINATION_PATH;
    private $_allowed_types = CREA_IMAGENES_ALLOWED_TYPES;
    
    public function __construct()
    {
        parent::Model();
    }
    
    private function _resize_image()
    {
        $return = FALSE;
        
        $config['image_library']    = 'gd2';
        $config['source_image']        = $this->_source_path;
        $config['new_image']        = $this->_destination_path.$this->_new_filename;
        $config['create_thumb']        = TRUE;
        $config['mantain_ratio']    = TRUE;
        $config['width']            = $this->_width;
        $config['height']            = $this->_height;
        
        $this->load->library('image_lib', $config);
        
        $return = $this->image_lib->resize();
        $this->image_lib->clear();
        
        return $return;
    }
    
    public function resize($source_path, $filename, $type, $newfilename, $width, $height, $destination_path)
    {
        switch($type) {
            case "producto":
                if(!$destination_path)
                    $this->_destination_path = CREA_IMAGENES_DESTINATION_PATH_PRODUCTS;
                break;
        }
        
        if(!$source_path)
            return FALSE;
        else
            $this->_source_path = $source_path;
        
        if(!$filename)
            return FALSE;
        else
            $this->_filename = $filename;
            
        if($newfilename)
            $this->_new_filename = $newfilename;
        else
            $this->_new_filename = $this->_filename;
            
        if($width)
            $this->_width = $width;
            
        if($height)
            $this->_height = $height;
            
        if($destination_path)
            $this->_destination_path = $destination_path;
            
        return $this->_resize_image($filename);
    }
    
    public function upload()
    {
        $config['upload_path'] = $this->_upload_path;
        $config['allowed_types'] = $this->_allowed_types;
        
        $this->load->library('upload', $config);
        
        if(!$this->upload->do_upload())
        {
            $data = array(
                'return' => FALSE,
                'message' => $this->upload->display_errors()
            );
            
            return $data;
        }
        
        else {
            $results = $this->upload->data();
            
            $data = array(
                'return'            => TRUE,
                'source_image'        => $results['full_path'],
                'file_path'            => $results['file_path'],
                'file_name'            => $results['file_name'],
                'raw_name'            => $results['raw_name'],
                'file_ext'            => $results['file_ext'],
                'original_width'    => $results['original_width'],
                'original_height'    => $results['original_height']
            );
            
            return $results;
        }
    }
}

A print_r of the $_FILE:
Code:
Array
(
    [imagen] => Array
        (
            [name] => wandroid_001.png
            [type] => image/png
            [tmp_name] => C:\wamp\tmp\php9535.tmp
            [error] => 0
            [size] => 718806
        )

)

And the form_upload I use to upload:
Code:
&lt;?php
$imagen = array(
    'name'    => 'imagen',
    'id'    => 'imagen',
    'size'    => 180,
    'value' => set_value('imagen')
);
?&gt;

<dt>&lt;?php echo form_label($this->lang->line('creactienda_admin_tienda_label_imagen'), $imagen['id']); ?&gt;</dt>
    <dd>
        &lt;?php echo form_upload($imagen); ?&gt;
        &lt;?php echo form_error($imagen['name']); ?&gt;
    </dd>

Anybody knows if its possible to use this upload inside the model and how?

Thank you in advance,
Isern
#2

[eluser]überfuzz[/eluser]
Yes it's possible. I can't really see what you mean, why would it be a problem..?
#3

[eluser]umefarooq[/eluser]
well to keep your code clean do image uploading and resizing in controller just send you file path to you model to save image name and path, keep you model only to deal with database, where in controller you can put you image uploading and resizing code, you can see you model code its quite difficult to understand the code.
#4

[eluser]Isern Palaus[/eluser]
[quote author="überfuzz" date="1259588090"]Yes it's possible. I can't really see what you mean, why would it be a problem..?[/quote]

I'm getting a error: You did not select a file to upload. with the code above. Sad

[quote author="umefarooq" date="1259590506"]well to keep your code clean do image uploading and resizing in controller just send you file path to you model to save image name and path, keep you model only to deal with database, where in controller you can put you image uploading and resizing code, you can see you model code its quite difficult to understand the code.[/quote]

Well... probably it's the way to go but I've to repeat this code on:

productos/productoAdd
productos/productoUpdate
fabricantes/fabricanteAdd
fabricantes/fabricanteUpdate
categorias/categoriaAdd
categorias/categoriaUpdate
gestion/envioUpdate
gestion/pagoUpdate

If I've to change a little thing... I've to change all the files. What I'm looking for is a way to have less code for future modifications. At this moment, my upload doesn't works in a model... which seems to me a nice way to organize it. You're right to mantain the model only to dial with database but what is a solution to organize the upload without repeting me out?

Thank you two,
Isern
#5

[eluser]Isern Palaus[/eluser]
Hey,

Okay I've solved. In the model you have to call: $this->upload->do_upload('name_of_form_upload') !

By the wayumefarooq, how you will organize a upload function for a lot of controllers without repeating it?

Thank you!
Isern
#6

[eluser]umefarooq[/eluser]
for that you can create a library with you code and settings just pass the file field name to your library function and get the file name or path back and you can use your custom made library over and over in you project. no need to repeat the code again and again in models or controllers.




Theme © iAndrew 2016 - Forum software by © MyBB