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


Messages In This Thread
Can I use the upload library inside a model? - by El Forum - 11-29-2009, 09:20 PM
Can I use the upload library inside a model? - by El Forum - 11-30-2009, 01:34 AM
Can I use the upload library inside a model? - by El Forum - 11-30-2009, 02:15 AM
Can I use the upload library inside a model? - by El Forum - 11-30-2009, 03:18 AM
Can I use the upload library inside a model? - by El Forum - 11-30-2009, 03:30 AM
Can I use the upload library inside a model? - by El Forum - 11-30-2009, 05:52 AM



Theme © iAndrew 2016 - Forum software by © MyBB