⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

How to change file name before uploading to database and upload path.



Suman Mp
  • profile picture
  • Member

Posted 18 December 2012 - 09:19 AM

Hello everyone ,

I need to rename for image before it uploading to database and directory.

victor
  • profile picture
  • Member

Posted 18 December 2012 - 09:40 AM

Hi Suman Mp and welcome to the forum. Can you give more information about your problem.

victor
  • profile picture
  • Member

Posted 18 December 2012 - 09:42 AM

Do you need to have different path for images?

Suman Mp
  • profile picture
  • Member

Posted 18 December 2012 - 09:56 AM

Following the code i am using to rename the file .


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;
}

}

Suman Mp
  • profile picture
  • Member

Posted 18 December 2012 - 10:20 AM

My intention is to rename file in both sides (database and directory)

Suman Mp
  • profile picture
  • Member

Posted 18 December 2012 - 10:21 AM

Now i am succeed with database

Michele Aparecida Teixeira
  • profile picture
  • Member

Posted 09 April 2016 - 21:05 PM

Hello !

Do You change the filename physically?
I'm having trouble in this case. The database is okay.
Tks !