Search code examples
phpmysqlcodeigniterfile-uploadgrocery-crud

checking file format during upload a file in grocery CRUD


I have the following code for file upload using grocery 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.
    }

Solution

  • Yes, Have you tried with following way:

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