我需要在文件上传时重命名文件并插入数据库。 我搜索方法,但我找不到合适的代码。 我试图使用回调但它没有用。
这是我的代码:
public function home()
{
$crud = new grocery_CRUD();
$crud->set_theme('datatables');
$crud->set_table('blog_post');
$crud->set_field_upload('post_image',UPLOAD_PATH);
$crud->callback_before_upload(array($this,'_before_upload'))
$crud->callback_before_insert(array($this,'rename_img_db'));
$output = $crud->render();
$this->_example_output($output);
}
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","doc","docx","pdf");
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;
}
}
答案 0 :(得分:0)
我注意到你的行中有一个便条:$ crud-> callback_before_upload(array($ this,'_ before_upload'))
此外,我必须做类似的事情并使用callback_after_insert,然后你可以获得$ primary_key变量并使用该更新元素,如下所示:
$crud->callback_after_insert(array($this, 'rename_img_db'));
public function rename_img_db($post_array,$primary_key)
{
//Here goes the get and set querys with your $primary_key
}