【问题标题】:File Upload in UserfrostingUserfrosting 中的文件上传
【发布时间】:2016-08-08 14:30:00
【问题描述】:

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

曾尝试在控制器中访问 $_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();
}
...

这会返回以下错误,

类型:InvalidArgumentException

消息:找不到键识别的上传文件:imagefile

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

行:139

相关的树枝块是

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

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

感谢任何帮助/指针。

谢谢!

【问题讨论】:

  • 第 139 行是这一行吗:$file = new \Upload\File('imagefile', $storage);?
  • 谢谢!不,它是#0 C:\xampp\htdocs\userfrosting\userfrosting\controllers\AccountController.php(424): Upload\File-&gt;__construct('imagefile', Object(Upload\Storage\FileSystem))(跟踪的第一行)。
  • 更新。发现使用独立的 php 处理程序上传文件,在 userfrosting 外部使用 userfrosting 的 MV。基于 Twig 的前端运行良好。 index.php 中的路由和控制器用于 GET 路由和对 userfrosting(在公共文件夹中)之外的处理程序的 Ajax 调用。

标签: slim userfrosting


【解决方案1】:

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

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

【讨论】:

  • 谢谢亚历克斯!是的!,我们正在使用g ufFormSubmit(基本上修改了register.twig)。非常感谢,会检查这些......上帝保佑你!
猜你喜欢
  • 1970-01-01
  • 2016-12-05
  • 1970-01-01
  • 2011-08-16
  • 2016-05-23
  • 2016-11-10
  • 2018-03-08
  • 2016-03-11
  • 1970-01-01
相关资源
最近更新 更多