0

I want to load datafixtures with DoctrineFixturesBundle with images but I don't know how to make it work.

I tried this one :

public function load(ObjectManager $manager)
{
    $imageGrabTail = new Image();
    $imageGrabTail->setTrick($this->getReference('grab-tail'));
    $imageGrabTail->setUpdatedAt(new \DateTimeImmutable());
    $file = new UploadedFile($imageGrabTail->getUploadDir() . '/63.jpeg', 'Image1', null, null, null);
    $imageGrabTail->setFile($file);
    $manager->persist($imageGrabTail);
    $manager->flush();

}

My method getUploadDir():

public function getUploadDir()
{
    return 'uploads/img';
}

But I have an error :

[Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException]  
  The file "uploads/img/63.jpeg" does not exist

My file 63.jpeg exists on this folder.

Is there someone who can explain to me why it's not working ?

Thanks !

Nathan Meyer
  • 312
  • 7
  • 18
  • 1
    Maybe you have the solution here https://stackoverflow.com/questions/12309077/how-would-you-add-a-file-upload-to-a-symfony2-datafixture using the File object – Smaïne Sep 12 '17 at 22:44
  • magic mentor ;). I've seen this in the afternoon, I don't know why, I could not make it work... I just tried again and it's working now ... Thx – Nathan Meyer Sep 12 '17 at 22:57

2 Answers2

0

This object UploadedFile is for file that are upload through a request. Here there is no request, so you have to use get_file_contents() function in order to access your data. Try this function and tell us the result of it.

  • With `$file = file_get_contents($imageGrabTail->getUploadDir().'/19.png', true);` I have error : `Warning: file_get_contents(uploads/img/19.png): failed to open stream: No such file or directory`, but the file exists ! – Nathan Meyer Sep 12 '17 at 22:42
  • I just tried with a `$file = file_exists($imageGrabTail->getUploadDir().'/19.png');` and it returns true – Nathan Meyer Sep 12 '17 at 22:47
0

To solve it I've just followed this thread

How would you add a file upload to a Symfony2 DataFixture?

Nathan Meyer
  • 312
  • 7
  • 18