How to upload files?

Hi, I am taking my first steps with GroceryCrud.
I have seen that the ‘set_field_upload’ function has been removed so what would be the best alternative to upload a file and still use GroceryCrud?
Thanks for your help!

By using Grocery Crud Enterprise :wink:

Hi @ariel ,
I am facing the same issue… Once I get a workaround I will post it for you :slight_smile:

I believe it can be done easily … especially if you used grocerygrud 1.x

Well, didn’t take me much time before achieving that…
I have written a working code. Of course it needs sanitizing … but it works

$crud = new GroceryCrud ();
$crud->setTable ('orders');
$crud->setSubject ('order', 'Orders');

// Oerride the default upload field ...
$crud->callbackAddField (
  'order_docs',
  function () {
    return '<input id="field-order_docs" type="file" class="form-control" name="order_docs" value="">';
  }
);

// Upload the file beforeInsert ...
$crud->callbackBeforeInsert (
  function ($cbData) {
    $toUpload = $this->request->getFile('order_docs');
    $toUpload->move(WRITEPATH . 'uploads');
    $cbData->data['order_docs'] = $toUpload->getName();
    return $cbData;
  }
);
// Render ...
$output = $crud->render ();

// Your uploaded files will be in CodeIgniter Directory '/writable/uploads'
1 Like

It works perfectly to add a new one, but when editing the field it is text type. Any idea how to solve the problem? Many thanks

Sorry for being away for this long. Of course when you edit it will be a text type… What do you want it to be? You need to override that … Have you solved your issue yet?

i try this and this is really helpful for me thank you for sharing your reply

thanks for sharing these codes,