Image upload with custom saving data

Hi,

I need upload images in my levels model with adding image data(name, generated unique name) to another table than my model is using in admin panel, and to the table what model using i need just to add id from that another table.

Another table :
CREATE TABLE IF NOT EXISTS `uploaded_files` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR (255) NOT NULL, `filename` VARCHAR (255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Table what model uses:

CREATE TABLE IF NOT EXISTS `levels` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR (255) NOT NULL, `rank` TINYINT NOT NULL, `file_id` INT (11) NOT NULL, `chantier_id` INT(11) NOT NULL, `active` ENUM('yes', 'no') NOT NULL DEFAULT 'yes', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Also can I save those files not to assets folder, but for example uploads?
After reading documentation I stucked with this issue and have no idea how I could do this. Can you help me with this one?

p.s. admin, you are doing great job.

Comments

  • edited 6:01AM
    To upload an image from your levels model, you'll need to add those file upload fields in your levels_model::form_fields method like so:

    ... $fields['image'] = array('type' => 'file', 'folder' => 'uploads'); // assets/uploads must be writable ...
    More on that field type can be found here:
    http://docs.getfuelcms.com/general/forms#file

    Then, you'll need to add an on_after_post hook to your levels_model that will save the image information to the uploaded_files table. The uploaded file information can be accessed from the upload_data model property. This property holds an array of all the uploaded image information returned from CI's File upload class (see the array of information at the bottom of this page https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html).

    Below is a pretty rough example to give you an idea (didn't test it out):
    function on_after_post($values){ $CI =& get_instance(); $CI->load->model('upload_files_model'); foreach($this->upload_data as $data) { $save['name'] = $data['file_name']; // not sure what the name value is $save['file_name'] = $data['file_name']; $id = $CI->upload_files_model->save($save); } $values['file_id'] = $id; $this->save($values); return $values; }
    More on hooks can be found here:
    http://docs.getfuelcms.com/general/models#hooks

    You may also want to add "uploads" => "uploads/" to the "$config['assets_folders']" found in the fuel/application/config/asset.php file
  • edited August 2014
    Thank you it works, just 1 more question, if i wona save uploaded file with another name, how can I implement that functionality?

    I implemented that like :
    public function on_after_post($values) { $CI =& get_instance(); $CI->load->model('upload_files_model'); $this->load->library('uploader'); foreach($this->upload_data as $data) { $save['filename'] = $data['file_name']; // not sure what the name value is $save['name'] = random_string('unique') . '' . date('Ymd') . '' . $data['file_name'] ; $id = $CI->upload_files_model->save($save); rename( assets_server_path('uploads/') . '' . $save['filename'] . '' , assets_server_path('uploads/') . '' . $save['name'] . '' ); } $values['file_id'] = $id; $this->save($values); return $values; }

    I there better way to do that?
  • edited 6:01AM
    There is a "file_name" parameter you can set in your image file field. The file field type can also have placeholder values that will look at the post values upon submit to substitute:
    http://docs.getfuelcms.com/general/forms#file
Sign In or Register to comment.