【发布时间】: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->__construct('imagefile', Object(Upload\Storage\FileSystem))(跟踪的第一行)。 -
这里是文件/行 - [link]github.com/brandonsavage/Upload/blob/develop/src/Upload/…
-
更新。发现使用独立的 php 处理程序上传文件,在 userfrosting 外部使用 userfrosting 的 MV。基于 Twig 的前端运行良好。 index.php 中的路由和控制器用于 GET 路由和对 userfrosting(在公共文件夹中)之外的处理程序的 Ajax 调用。
标签: slim userfrosting