2

I'm having this api that's supposed to upload a file. The code was designed to look for the uploaded file where it should be, in $request->files but that variable is empty. Instead, I find the image in $content in it's character form(����). Is this behaviour faulty or am I missing something? I checked composer.json but no upload(Vich or DoctrineExtensions) bundle is included nor configured. I could set up one of them but I don't know if the current behaviour is wrong by nature or the previous developer knew something more about this than I do?

For the record, I have the yaml file for the File class that looks similar to the one from 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: the files are supposed to be saved as files, not a blobs in database.

Matt
  • 127
  • 12

1 Answers1

0

It looks like the client sends the file in the body of a typical HTTP request instead of using a multipart request. Symfony's $request->files (and generally speaking everything using PHP's $_FILES under the hood) only works with multipart request.

You can either, fix your client to send multipart requests (and benefit from security protection provided by PHP) or deal with the raw content by yourself by doing something like:

// 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());
Kévin Dunglas
  • 2,864
  • 2
  • 23
  • 39