symfonysonatasonata-media-bundle

SonataMedia Bundle: Create Media Entity throws "is_file() expects parameter 1 to be a valid path, string given"


I want to create an image media entity (using SonataMediaBundle) with the following code:

$media = new Media();
$media->setBinaryContent($binaryImageContent);
$media->setContext('default');
$media->setProviderName('sonata.media.provider.image');

$this->mediaManager->save($media);

The $binaryImageContent is created like this:

$binaryContent = file_get_contents($filePath);

The $filepath file physically exists.

However instead of creating the media entity I always get the following warning:

Warning: is_file() expects parameter 1 to be a valid path, string given

As the error results within the library I'm not quite sure how to solve this. I'm working in a docker environment so I thought it might also be triggered by some permission problems but the same error occurs on production.


Solution

  • Okay well ... the solution was to create the binary content as follows:

    $binaryContent = new File($filePath);
    $imageMediaManager->createImageMediaObject($binaryContent);
    

    Then it's working (on production not on docker but that's another prob)