【问题标题】:File uploaded as blob文件作为 blob 上传
【发布时间】:2017-07-17 12:33:36
【问题描述】:

我有这个应该上传文件的 api。该代码旨在在$request->files 中查找应在何处上传的文件,但该变量为空。相反,我在$content 中以字符形式(����)找到图像。这种行为是错误的还是我错过了什么? 我检查了composer.json,但没有包含或配置上传(Vich 或 DoctrineExtensions)包。我可以设置其中一个,但我不知道当前的行为是天生错误的,还是以前的开发人员比我更了解这一点?

作为记录,我有 File 类的 yaml 文件,它看起来类似于来自 DoctrineExtensions - Uploadable 的文件

MyApp\FileBundle\Entity\File:
    type: entity
    table: file_records
    repositoryClass: MyApp\FileBundle\Repository\FileRepository

    id:
        id:
          type: integer
          generator:
              strategy: AUTO

    fields:

        path:
          name: path
          type: string

        name:
          name: string
          type: string

        mimeType:
          name: mimeType
          type: string
          nullable: true

        size:
          name: size
          type: decimal
          nullable: true

        initialName:
          name: initial_name
          type: string

Ps:文件应该保存为文件,而不是数据库中的 blob。

【问题讨论】:

    标签: php symfony file-upload doctrine symfony-3.2


    【解决方案1】:

    看起来客户端在典型的 HTTP 请求正文中发送文件,而不是使用多部分请求。 Symfony 的 $request->files(一般来说,所有使用 PHP 的 $_FILES 的东西)只适用于多部分请求。

    您可以修复您的客户端以发送多部分请求(并受益于 PHP 提供的安全保护)或通过执行以下操作自行处理原始内容:

    // Be careful to check that the real content is not PHP or something harmful this before 
    file_put_contents('/path/to/myimage.png', $request->getContent());
    

    【讨论】:

      猜你喜欢
      • 2014-03-29
      • 2011-10-03
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      • 2014-01-23
      • 2017-03-14
      • 2019-01-20
      相关资源
      最近更新 更多