title: Symfony HttpFoundation UploadedFile "not uploaded due to unknown error" when using Doctrine DataFixtures
body: <p>I've been using my Attachment entity based on the cookbook recipie <a href="http://symfony.com/doc/2.3/cookbook/doctrine/file_uploads.html" rel="nofollow">How To Handle File Uploads With Doctrine</a> in Symfony 2.3.</p>
  
  <p>It works well, even in functional tests. However using it with Doctrine DataFixtures is causing me problems.</p>
  
  <blockquote>
    <p><code>[Symfony\Component\HttpFoundation\File\Exception\FileException]<br>
    The file "o-rly-copy.jpg" was not uploaded due to an unknown error.</code></p>
  </blockquote>
  
  <p>This was not helpful, however I did run <code>php app/console doctrine:fixtures:load -v</code> to bring up a stack trace and it appears the exception is thrown not on the persisting method, but on <code>$manager-&gt;flush()</code></p>
  
  <p><code>Attachment::setFile()</code> requires an instance of <code>UploadedFile</code> so I wonder if there is a way round that.</p>
  
  <p>It appears the error occurs on line 225 of <code>Symfony\Component\HttpFoundation\File\UploadedFile</code></p>
  
  <pre><code>return $this-&gt;test ? $isOk : $isOk &amp;&amp; is_uploaded_file($this-&gt;getPathname())
  </code></pre>
  
  <p>The condition for <code>is_uploaded_file()</code> returns <code>false</code> because the file was already on the server. </p>
  
  <pre><code>&lt;?php
  
  /**
   * Prepopulate the database with image attachments.
   */
  final class AttachmentFixtures extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
  {
      private static $imageData = array(
          array(
              'name' =&gt; "O RLY?",
              'file' =&gt; "o-rly",
              'type' =&gt; "jpg",
          ),
          //...
      );
  
      public function getPathToImages()
      {
          return $this-&gt;container-&gt;get('kernel')-&gt;getRootDir() . '/../src/Acme/DemoBundle/Resources/public/default/images';
      }
  
      public function getPathToUploads()
      {
          return $this-&gt;container-&gt;get('kernel')-&gt;getRootDir() . '/../web/uploads/fixtures';
      }
  
      /**
       * {@inheritDoc}
       */
      public function load(ObjectManager $manager)
      {
          $imageReferences = array();
          $filesystem = $this-&gt;container-&gt;get('filesystem');
  
          foreach (self::$imageData as $image) {
              $imageFilename         = sprintf('%s.%s',      $image['file'], $image['type']);
              $copiedImageFilename   = sprintf('%s-copy.%s', $image['file'], $image['type']);
  
              $pathToImageFile = sprintf('%s/%s', $this-&gt;getPathToImages(), $imageFilename);
  
              try {
                  $filesystem-&gt;copy($pathToImageFile, $pathToCopiedFile = sprintf('%s/%s', $this-&gt;getPathToUploads(), $copiedImageFilename));
                  $filesystem-&gt;chmod($pathToCopiedFile, 0664);
              } catch (IOException $e) {
                  $this-&gt;container-&gt;get('logger')-&gt;err("An error occurred while copying the file or changing permissions.");
              }
  
              $imageFile = new UploadedFile(
                  $pathToCopiedFile,                                              // The full temporary path to the file
                  $copiedImageFilename,                                           // The original file name
                  'image/' . 'jpg' === $image['type'] ? 'jpeg' : $image['type'],  // Mime type - The type of the file as would be provided by PHP
                  filesize($pathToCopiedFile),
                  null,
                  null,
                  true
              );
  
              $imageAttachment = new Attachment();
  
              $imageAttachment-&gt;setName($image['name']);
              $imageAttachment-&gt;setFile($imageFile);
  
              // Populate a reference array for later use
              $imageReferences['attachment-'.$image['file']] = $imageAttachment;
  
              $manager-&gt;persist($imageAttachment);
          }
  
          $manager-&gt;flush(); // &lt;-- Exception throw here
  
  
          // Create references for each image to be used by other entities that
          // maintain a relationship with that image.
          foreach ($imageReferences as $referenceName =&gt; $image) {
              $this-&gt;addReference($referenceName, $image);
          }
      }
  }
  </code></pre>