【问题标题】:php slim how to file upload using eloquent?php slim如何使用雄辩的文件上传?
【发布时间】:2017-11-01 20:35:11
【问题描述】:

我正在尝试使用邮递员上传图片,但图片未插入数据库中

这是错误

对我应该怎么做有什么建议吗?

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'image' cannot be null (SQL: insert into `images` (`image`) values ())

这是路线

$app->post('/images', function($request, $response, $args) {
  $data = $request->getUploadedFiles();
  $image = new Image();
  $image->image = $data['image'];
  $image->save();

  return $this->response->write($image->toJson());
});    

这里是邮递员页面

这里是标题

【问题讨论】:

  • 您确定邮递员在请求中正确发送了图片吗?
  • 你能分享一下你是如何使用邮递员来测试api调用的吗?
  • @bos570 我刚刚编辑了它的样子
  • 能分享一下标题吗?
  • @bos570 我刚刚更新了,现在看看,谢谢:)

标签: php eloquent slim


【解决方案1】:

问题

您将模型图像属性设置为\Psr\Http\Message\UploadedFileInterface 类型的对象,而不是字符串。模型不知道如何处理对象。

Slim $request->getUploadedFiles() 返回一个 \Psr\Http\Message\UploadedFileInterface 类型的对象,并且不是字符串

解决方案

要访问图像数据,首先我们需要获取

$data = $request->getUploadedFiles();
$uploadedImage = $data['image'];
$uploadedImageStream = $uploadedImage->getStream();

现在我们有了,我们可以像读取文件一样从它读取。但是,我们只是一次获取所有数据并将其设置为 Model 属性:

$image->image = $uploadedImageStream->getContents();

【讨论】:

  • 谢谢,但我知道了Call to a member function getStream() on null
  • 我怀疑图像键不匹配。检查您的邮递员正文密钥。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-10
  • 2018-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多