Userfrosting 中的文件上传

File Upload in Userfrosting

我们需要让用户上传图片作为注册过程的一部分。

曾尝试在控制器中访问 $_FILES['filename'],结果在 slim 下未定义。

在几篇文章中看到了 Slim 的文件上传方式,据说可以,但我碰壁了。

树枝部分与 Bootstrap File Input library

配合得很好

服务器部分使用File Upload library for Slim

控制器代码(对 AccountController 的修改)如下所示

...
$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();
}
...

这个returns下面的错误,

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

相关的树枝块是

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

有没有人能够让文件上传作为任何 Userfrosting 代码的一部分工作?

感谢任何帮助/指点。

谢谢!

我的猜测是您正在使用 ufFormSubmit 来提交您的注册表格,它并没有抓取文件输入。因此,您可能需要在客户端添加一些额外的代码,以显式提交文件输入以及表单的其余部分。请参阅使用 Dropzone 和 UF 的示例:https://gist.github.com/frostbitten/c1dce70023321158a2fd#file-upload-twig

顺便说一句,您可以使用浏览器查看 实际上 在您的 POST 请求中发送的数据。例如,在 Firefox 中,您可以使用 Network Monitor.