[attachment=910:Screenshot from 2015-03-16 11:23:19.png]
File upload url error on edit record
- Single Page
Posted 16 March 2015 - 09:24 AM
Posted 17 March 2015 - 08:45 AM
all i want to do is to divide my uploads into more than one folder because i have more uploads and i don't want to put them all in the same folder, can any one tell me how to do that ?
this is my code that has an issue in image url while editing the record
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Designer extends MY_Controller {
public function index(){
// CRUD table
$this->load->model('designer_model');
$this->load->helper('crud');
$this->load->helper('my_dir_helper');
$crud = generate_crud('designers');
if($this->uri->segment(4) && $crud->getState() == 'success'){
redirect('crop/image_crop/designer/'.$this->uri->segment(4));
}
$crud->columns('title', 'description', 'category_id', 'media_count', 'image');
$crud->display_as('media_count', 'Media Count')
->display_as('category_id', 'Category');
// sert relation
$crud->set_relation('category_id', 'designer_categories', 'title');
$crud->required_fields('title', 'category_id', 'image');
$crud->field_type('slug', 'invisible');
$crud->field_type('media_count', 'invisible');
$crud->field_type('created', 'invisible');
$crud->field_type('updated', 'invisible');
$crud->field_type('views', 'invisible');
$crud->field_type('likes', 'invisible');
$crud->field_type('image_path', 'invisible');
$crud->set_lang_string('form_update_changes','Update designer')
->set_lang_string('form_update_and_go_back','Update and go to crop image')
->set_lang_string('form_save_and_go_back','Save and go to crop image')
->set_lang_string('form_save','Save designer');
// prepare for file upload
mkdir_if_not_exists('assets/uploads/fashion-media/'.date('Y').'/'.date('m').'/'.date('d'));
$crud->set_field_upload('image','assets/uploads/fashion-media/'.date('Y').'/'.date('m').'/'.date('d'));
// callback functions
$crud->callback_before_insert(array($this, 'callback_before_create_designer'));
$crud->callback_before_update(array($this, 'callback_before_update_designer'));
$crud->callback_after_upload(array($this, 'callback_after_upload_designer'));
$crud->callback_column('image',array($this,'callback_image_url'));
$this->mTitle = "Designers";
$this->mViewFile = '_partial/crud';
$this->mViewData['crud_data'] = $crud->render();
}
/**
* Add created and updated fields on insert
*/
public function callback_before_create_designer($post_array){
$post_array['slug'] = url_title($post_array['title'], 'dash', TRUE);
$post_array['image_path'] = 'assets/uploads/fashion-media/'.date('Y').'/'.date('m').'/'.date('d');
$post_array['created'] = date('Y-m-d H:i:s');
$post_array['updated'] = date('Y-m-d H:i:s');
return $post_array;
}
/**
* Change updated field with current time on update
*/
public function callback_before_update_designer($post_array){
$post_array['slug'] = url_title($post_array['title'], 'dash', TRUE);
$post_array['updated'] = date('Y-m-d H:i:s');
return $post_array;
}
public function callback_image_url($value, $row){
return '<a href="'.base_url($row->image_path.'/'.$row->image).'" class="image-thumbnail"><img src="'.base_url($row->image_path.'/athumb_'.$row->image).'" height="50px"></a>';
}
/**
* callback function to create automatic thumb after uploading the image
*/
public function callback_after_upload_designer($uploader_response,$field_info, $files_to_upload){
$this->load->library('image_moo');
//Is only one file uploaded so it ok to use it with $uploader_response[0].
$file_uploaded = 'assets/uploads/fashion-media/'.date('Y').'/'.date('m').'/'.date('d') .'/'.$uploader_response[0]->name;
if($this->image_moo->load($file_uploaded)->resize_crop(323,283)->save_pa('athumb_')){
return true;
}else{
return false;
}
}
}
when you edit a designer that his image uploaded any day before today i get preview error (you can check image above in the topic)
Posted 20 March 2015 - 20:01 PM
well that is bound to happen because you are uploading it correctly but presrving it the wrong way.
instead you need to write a callback to move the uploaded file to the required directory. Post that is done - update the record with the path (ex: the date) ... thats the only additional path required..
by default your upload path should have been set to the folder just before the date.
So when u edit / list - it will append the date and the image name in the path - thats it.. you done with..
Happy GCing:)
Posted 23 March 2015 - 12:17 PM
Many thanks for your reply.
my solution
i checked the current state and on edit i saved the current record image path into a session and use this path to set the upload file and on add state i used the default upload path that works for me .. check the code below
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Designer extends MY_Controller {
public function index(){
// CRUD table
$this->load->model('designer_model');
$this->load->helper('crud');
$this->load->helper('my_dir_helper');
$crud = generate_crud('designers');
// state and state info
$state = $crud->getState();
$state_info = $crud->getStateInfo();
// save current designer image name in a session to update it in the db if it is changed
if($state == 'edit' && $this->uri->segment(4)){
$current = $this->designer_model->get($this->uri->segment(4));
$this->session->set_userdata('last_image_name', $current['image']);
$this->session->set_userdata('last_image_path', $current['image_path']);
}
// redirect to manual crop image from page
if($state == 'success' && $this->uri->segment(4)){
redirect('crop/image_crop/designer/'.$state_info->primary_key);
}
$crud->columns('title', 'description', 'category_id', 'media_count', 'image', 'move_up_down');
$crud->display_as('media_count', 'Media Count')
->display_as('category_id', 'Category');
// sert relation
$crud->set_relation('category_id', 'designer_categories', 'title');
$crud->required_fields('title', 'category_id', 'image');
$crud->field_type('slug', 'invisible');
$crud->field_type('media_count', 'invisible');
$crud->field_type('created', 'invisible');
$crud->field_type('updated', 'invisible');
$crud->field_type('views', 'invisible');
$crud->field_type('likes', 'invisible');
$crud->field_type('image_path', 'invisible');
$crud->field_type('priority', 'invisible');
// designer ordering
$crud->callback_column('move_up_down', array($this, 'populate_up_down'));
$crud->order_by('priority', 'desc');
$this->session->set_userdata('callableAction', site_url(). '/designer/updatePosition/designers');
$this->session->set_userdata('primary_key', 'id');
$crud->set_js("backend.php/designer/dragdrop_js");
$crud->set_lang_string('form_update_changes','Update designer')
->set_lang_string('form_update_and_go_back','Update and go to crop image')
->set_lang_string('form_save_and_go_back','Save and go to crop image')
->set_lang_string('form_save','Save designer');
// prepare for file upload
mkdir_if_not_exists('assets/uploads/fashion-media/'.date('Y').'/'.date('m').'/'.date('d'));
// set file upload path according to the current controller state
if($state == 'add' || $state == 'upload_file'){
$crud->set_field_upload('image','assets/uploads/fashion-media/'.date('Y').'/'.date('m').'/'.date('d'));
}elseif($state == 'edit' || $state == 'delete_file'){
$crud->set_field_upload('image', $this->session->userdata('last_image_path'));
// delete the auto and manual thumb on deleting the original file
if($state == 'delete_file') {
// auto thumb
if(file_exists($this->session->userdata('last_image_path') . '/athumb_' . $this->session->userdata('last_image_name'))){
unlink($this->session->userdata('last_image_path') . '/athumb_' . $this->session->userdata('last_image_name'));
}
// manual thumb
if(file_exists($this->session->userdata('last_image_path') . '/mthumb_' . $this->session->userdata('last_image_name'))){
unlink($this->session->userdata('last_image_path') . '/mthumb_' . $this->session->userdata('last_image_name'));
}
}
}else{
$crud->set_field_upload('image','assets/uploads/fashion-media/'.date('Y').'/'.date('m').'/'.date('d'));
}
// callback functions
$crud->callback_before_insert(array($this, 'callback_before_create_designer'));
$crud->callback_after_insert(array($this, 'callback_after_create_designer'));
$crud->callback_before_delete(array($this, 'callback_before_delete_designer'));
$crud->callback_before_update(array($this, 'callback_before_update_designer'));
$crud->callback_after_upload(array($this, 'callback_after_upload_designer'));
$crud->callback_column('image',array($this,'callback_image_url'));
$this->mTitle = "Designers";
$this->mViewFile = '_partial/crud';
$this->mViewData['crud_data'] = $crud->render();
}
}
?>
again thanks for this awesome library it saved my time
