title: Media creation via php in Shopware 6
body: <p>i'm struggling get a media import via PHP for Shopware 6 to work.</p>
  <p>This is my service:</p>
  <pre><code>&lt;?php declare(strict_types=1);
  
  namespace My\Namespace\Service;
  
  use Shopware\Core\Content\Media\File\MediaFile;
  use Shopware\Core\Content\Media\MediaService;
  use Shopware\Core\Framework\Context;
  
  class ImageImport
  {
      /**
       * @var MediaService
       */
      protected $mediaService;
  
      /**
       * ImageImport constructor.
       * @param MediaService $mediaService
       */
      public function __construct(MediaService $mediaService)
      {
          $this-&gt;mediaService = $mediaService;
      }
  
  
      public function addImageToProductMedia($imageUrl, Context $context)
      {
          $mediaId = NULL;
          $context-&gt;disableCache(function (Context $context) use ($imageUrl, &amp;$mediaId): void {
              $filePathParts = explode('/', $imageUrl);
              $fileName = array_pop($filePathParts);
              $fileNameParts = explode('.', $fileName);
  
              $actualFileName = $fileNameParts[0];
              $fileExtension = $fileNameParts[1];
  
  
              if ($actualFileName &amp;&amp; $fileExtension) {
                  $tempFile = tempnam(sys_get_temp_dir(), 'image-import');
                  file_put_contents($tempFile, file_get_contents($imageUrl));
  
                  $fileSize = filesize($tempFile);
                  $mimeType = mime_content_type($tempFile);
  
                  $mediaFile = new MediaFile($tempFile, $mimeType, $fileExtension, $fileSize);
  
                  $mediaId = $this-&gt;mediaService-&gt;saveMediaFile($mediaFile, $actualFileName, $context, 'product');
              }
          });
          return $mediaId;
      }
  }
  </code></pre>
  <p>A entry in the table media with the correct media_folder_association is created. And as far as i can see there are no differences to other medias uploaded via backend (except private is 1 and user_id is NULL).</p>
  <p>But in the backend the media entries are broken, seems like it can not load the actual image file (i've tried to set private to true to see it in the media section, same happens when adding the media to a product via php, but i guess the problem is before any assignment to products).</p>
  <p><a href="https://i.stack.imgur.com/LEdJK.png" rel="nofollow noreferrer">Image in backend media</a></p>
  <p>Has anybody a suggestion whats wrong here?</p>
  <p>Thanks
  Phil</p>
  <p>===== SOLUTION ======</p>
  <p>Here is the updated and working service:</p>
  <pre><code>&lt;?php declare(strict_types=1);
  
  namespace My\Namespace\Service;
  
  use Shopware\Core\Content\Media\File\FileSaver;
  use Shopware\Core\Content\Media\File\MediaFile;
  use Shopware\Core\Content\Media\MediaService;
  use Shopware\Core\Framework\Context;
  
  class ImageImport
  {
      /**
       * @var MediaService
       */
      protected $mediaService;
  
      /**
       * @var FileSaver
       */
      private $fileSaver;
  
      /**
       * ImageImport constructor.
       * @param MediaService $mediaService
       * @param FileSaver $fileSaver
       */
      public function __construct(MediaService $mediaService, FileSaver $fileSaver)
      {
          $this-&gt;mediaService = $mediaService;
          $this-&gt;fileSaver = $fileSaver;
      }
  
  
      public function addImageToProductMedia($imageUrl, Context $context)
      {
          $mediaId = NULL;
          $context-&gt;disableCache(function (Context $context) use ($imageUrl, &amp;$mediaId): void {
              $filePathParts = explode('/', $imageUrl);
              $fileName = array_pop($filePathParts);
              $fileNameParts = explode('.', $fileName);
  
              $actualFileName = $fileNameParts[0];
              $fileExtension = $fileNameParts[1];
  
  
              if ($actualFileName &amp;&amp; $fileExtension) {
                  $tempFile = tempnam(sys_get_temp_dir(), 'image-import');
                  file_put_contents($tempFile, file_get_contents($imageUrl));
  
                  $fileSize = filesize($tempFile);
                  $mimeType = mime_content_type($tempFile);
  
                  $mediaFile = new MediaFile($tempFile, $mimeType, $fileExtension, $fileSize);
                  $mediaId = $this-&gt;mediaService-&gt;createMediaInFolder('product', $context, false);
  
                  $this-&gt;fileSaver-&gt;persistFileToMedia(
                      $mediaFile,
                      $actualFileName,
                      $mediaId,
                      $context
                  );
              }
          });
          return $mediaId;
      }
  }
  </code></pre>