slimuserfrosting

File Upload in Userfrosting


We need to have the user upload an image as a part of sign up process.

Had tried accessing $_FILES['filename'] in the controller, which turns out to be undefined under slim.

Had seen about Slim's way of file uploading in a couple of articles, which are reported to be working, but I hit the wall.

The twig part works fine with Bootstrap File Input library

For the server part using File Upload library for Slim

Controller code (modifications to AccountController) looks like this

...
$storage = new \Upload\Storage\FileSystem('c:/xampp/htdocs/userfrosting/public/images/');
$file = new \Upload\File('imagefile', $storage);

$new_filename = 'test.jpg';
$file->setName($new_filename);

$file->addValidations(array(
    // Ensure file is of type "image/jpg"
    new \Upload\Validation\Mimetype('image/jpg'),
    // Ensure file is no larger than 500K (use "B", "K", M", or "G")
    new \Upload\Validation\Size('500K')
));

// Access data about the file that has been uploaded
$uploadfiledata = array(
    'name' => $file->getNameWithExtension(),
    'extension' => $file->getExtension(),
    'mime' => $file->getMimetype(),
    'size' => $file->getSize(),
    'md5' => $file->getMd5(),
    'dimensions' => $file->getDimensions()
);
error_log('$uploadfiledata' . print_r($uploadfiledata, true));
// Try to upload file
try {
    // Success!
    $file->upload();
} catch (\Exception $e) {
    // Fail!
    $errors = $file->getErrors();
}
...

This returns the following error,

Type: InvalidArgumentException

Message: Cannot find uploaded file identified by key: imagefile

File: C:\xampp\htdocs\userfrosting\userfrosting\vendor\codeguy\upload\src\Upload\File.php

Line: 139

The relevant twig chunk is

<input id="imagefile" type="file" name="imagefile"  class="file" data-show-upload="false">

Has anyone been able to get file upload working as a part of any Userfrosting code?

Appreciate any help / pointers.

Thanks!


Solution

  • My guess is that you're using ufFormSubmit to submit your registration form, and it is not grabbing the file input. So, you will probably need to add some extra code on the client side to explicitly submit the file input along with the rest of the form. See this example using Dropzone and UF: https://gist.github.com/frostbitten/c1dce70023321158a2fd#file-upload-twig

    By the way, you can use your browser to see what data is actually being sent in your POST request. For example, in Firefox you can use the Network Monitor.