2

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!

Spurgeon
  • 148
  • 5
  • 12
  • Is line 139 this line: `$file = new \Upload\File('imagefile', $storage);`? – alexw Aug 09 '16 at 03:38
  • Thanks! No. It is `#0 C:\xampp\htdocs\userfrosting\userfrosting\controllers\AccountController.php(424): Upload\File->__construct('imagefile', Object(Upload\Storage\FileSystem))` (first line of trace). – Spurgeon Aug 09 '16 at 11:03
  • here is the file / line - [link]https://github.com/brandonsavage/Upload/blob/develop/src/Upload/File.php#L139 – Spurgeon Aug 09 '16 at 11:11
  • An update. Found that file upload with a stand alone php handler, outside userfrosting works with MV of userfrosting. Twig based frontend works well. Route and controller in index.php for GET route and Ajax call to handler outside userfrosting (in public folder) works. – Spurgeon Aug 10 '16 at 10:31

1 Answers1

3

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.

alexw
  • 8,468
  • 6
  • 54
  • 86
  • Thanks Alex! yes!, we are usingg ufFormSubmit (basically modification of register.twig). Thanks a ton, will check these out.. God bless you! – Spurgeon Aug 13 '16 at 05:43