在杂货店CRUD上传文件时检查文件格式

时间:2012-09-22 08:30:12

标签: php mysql codeigniter file-upload grocery-crud

我有使用杂货CRUD上传文件的以下代码

function test(){
        $crud=new grocery_CRUD();       
        $crud->set_table('test');
        $crud->add_fields('name', 'image');
        $crud->set_field_upload('image', 'test');
        $crud->callback_before_upload('image', array($this, 'before_test_upload'));
        $output=$crud->render();
        $output->page_title="Test Page";
        $this->_crud_output($output);       
    }
function before_test_upload($files_to_upload, $field_info){
        // Here I want to check the file format before that file upload to source folder.
    }

1 个答案:

答案 0 :(得分:6)

是的,您是否尝试过以下方式:

function employees_management()
{
    $crud = new grocery_CRUD();


    $crud->set_table('employees');
    $crud->set_relation('officeCode','offices','city');
    $crud->display_as('officeCode','Office City');
    $crud->set_subject('Employee');

    $crud->set_field_upload('file_url','assets/uploads/files');
    $crud->callback_before_upload(array($this,'example_callback_before_upload'));

    $output = $crud->render();

    $this->_example_output($output);
}    


function example_callback_before_upload($files_to_upload,$field_info)
{
/*
 * Examples of what the $files_to_upload and $field_info will be:    
$files_to_upload = Array
(
        [sd1e6fec1] => Array
        (
                [name] => 86.jpg
                [type] => image/jpeg
                [tmp_name] => C:\wamp\tmp\phpFC42.tmp
                [error] => 0
                [size] => 258177
        )

)

$field_info = stdClass Object
(
        [field_name] => file_url
        [upload_path] => assets/uploads/files
        [encrypted_field_name] => sd1e6fec1
)

*/

    foreach($files_to_upload as $value) {
        $ext = pathinfo($value['name'], PATHINFO_EXTENSION);
    }

    $allowed_formats = array("jpg","gif");
    if(in_array($ext,$allowed_formats))
    {
        return true;
    }
    else
    {
        return 'Wrong file format';    
    }

}