callbackUpload(string $fieldName, callable $callback)

The callbackUpload is used when we need to replace the default upload functionality of Grocery CRUD Enterprise.

For example the below code will show us an error at the page (so we can dump the data after the upload):

$crud->callbackUpload(function ($test1 = null) {

    return false;

will result the below:

  public 'field_name' => string 'photo_url' (length=14)
array (size=1)
  'photo_url' => 
    array (size=5)
      'name' => string 'photo-1191.jpg' (length=14)
      'type' => string 'image/jpeg' (length=10)
      'tmp_name' => string '/tmp/php/phpxSfeuZ' (length=19)
      'error' => int 0
      'size' => int 6756

Please have in mind that this is completely replacing the functionality of the default upload. That means that once you use this function you will need to make sure that:

For example you can see a full working example below. The example is pretty big as it handles all of the above bullet points.

// At this example we are using the library of 
// so please make sure that if you are going to use the below code,
// you will need to install this library first

$crud->callbackUpload(function ($uploadData)  {
    // Hardcoded paths. Please make sure that in case you just copy the below code 
    // that you replace these two variables with yours
    $uploadPath = '/real/upload/path/'; // directory of the drive
    $publicPath = '/Uploads/'; // public directory (at the URL)

    $fieldName = $uploadData->field_name;

    $storage = new \Upload\Storage\FileSystem($uploadPath);
    $file = new \Upload\File($fieldName, $storage);

    $filename = isset($_FILES[$fieldName]) ? $_FILES[$fieldName]['name'] : null;

    if ($filename === null) {
        return false;

    // The library that we are using want us to remove the file 
    // extension as it is adding it by itself!
    $filename = preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename);
    // Replace illegal characters with an underscore
    $filename = preg_replace("/([^a-zA-Z0-9\-\_]+?){1}/i", '_', $filename);


    // Validate file upload
        new \Upload\Validation\Extension(['gif', 'jpeg', 'jpg', 'png']),
        new \Upload\Validation\Size('20M')

    // Work around so the try catch will work as expected.
    // Upload file will not yield any error if the error_reporting is 0
    $display_errors = ini_get('display_errors');
    $error_reporting = error_reporting();
    ini_set('display_errors', 'on');

    // Upload file
    try {
        // Success!

    } catch (\Upload\Exception\UploadException $e) {
        // Upload error, return a custom message
        $errors = print_r($file->getErrors(), true);
        return (new \GroceryCrud\Core\Error\ErrorMessage())
                     ->setMessage("There was an error with the upload:\n" . $errors);
    } catch (\Exception $e) {
        throw $e;

    ini_set('display_errors', $display_errors);

    $filename = $file->getNameWithExtension();

    // Make sure that you return the results
    $uploadData->filePath = $publicPath . '/' . $filename;
    $uploadData->filename = $filename;

    return $uploadData;