【问题标题】:grocery crud rename filename on upload杂货杂货重命名上传时的文件名
【发布时间】:2014-12-14 22:23:21
【问题描述】:

我需要在文件上传和插入数据库时​​重命名文件。 我在寻找方法,但找不到正确的代码。 我尝试使用回调,但它不起作用。

这是我的代码:

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

【问题讨论】:

    标签: codeigniter file-upload grocery-crud


    【解决方案1】:

    我注意到您的行中有一个提示:$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
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多