How to change file name before uploading to database and upload path.
- Single Page
Posted 18 December 2012 - 09:19 AM
I need to rename for image before it uploading to database and directory.
Posted 18 December 2012 - 09:40 AM
Posted 18 December 2012 - 09:42 AM
Posted 18 December 2012 - 09:56 AM
public function home()
{
try{
$crud = new grocery_CRUD();
$crud->set_theme('datatables');
$crud->set_table('blog_post');
$crud->set_subject('Post');
$crud->required_fields('post_title','post_content');
$crud->set_field_upload('post_image',UPLOAD_PATH);
$crud->callback_before_upload(array($this,'_before_upload'));
$crud->columns('post_id','post_image','post_title','post_content','post_date','post_author');
$crud->add_fields('post_title','post_content','post_image','post_date','post_author');
$crud->callback_before_insert(array($this,'rename_img_db'));
$crud->edit_fields('post_title','post_content','post_image','post_date','post_author');
$output = $crud->render();
$this->_example_output($output);
}catch(Exception $e){
show_error($e->getMessage().' --- '.$e->getTraceAsString());
}
}
function rename_img_db($post_array)
{
if (!empty($post_array['post_image'])) {
$ext = end(explode(".",$post_array['post_image']));
$img_name = $post_array['post_image'] = mktime().".".$ext;
$post_array['post_image'] = $img_name;
}
return $post_array;
}
function _before_upload($files_to_upload,$field_info)
{
foreach($files_to_upload as $value) {
$ext = pathinfo($value['name'], PATHINFO_EXTENSION);
$rename = $value['name'];
}
$allowed_formats = array("jpg","gif","png");
if(in_array($ext,$allowed_formats))
{
return true;
}
else
{
return 'Wrong file format';
}
if ($rename) {
$ext1 = end(explode(".",$rename));
$img_name = $rename = mktime().".".$ext1;
$rename = $img_name;
return $rename;
}
}
Posted 18 December 2012 - 10:20 AM
Posted 18 December 2012 - 10:21 AM
Posted 09 April 2016 - 21:05 PM
Hello !