2017-09-12 33 views
0

我想加載帶有圖像的DoctrineFixturesBundle的datafixtures,但我不知道如何使它工作。如何使用symfony3中的文件加載datafixtures

我這個嘗試之一:

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(); 

} 

我的方法getUploadDir():

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

但我有一個錯誤:

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

我的文件63.jpeg這個存在夾。

有沒有人可以向我解釋爲什麼它不起作用?

謝謝!

+1

也許你在這裏解決https://stackoverflow.com/questions/12309077/how-would-you-add-a- file-upload-to-a-symfony2-datafixture使用File對象 –

+0

魔導師;)。我已經在下午看到了這一點,我不知道爲什麼,我無法使它工作...我只是再次嘗試,現在它的工作... Thx –

回答

0

該對象UploadedFile用於通過請求上傳的文件。這裏沒有請求,所以你必須使用get_file_contents()函數來訪問你的數據。 試試這個功能並告訴我們結果。

+0

With '$ file = file_get_contents($ imageGrabTail-> getUploadDir()。'/ 19.png',true);' 我有錯誤: 'Warning:file_get_contents(uploads/img/19.png):無法打開流:沒有這樣的文件或目錄,但文件存在! –

+0

我剛剛嘗試了一個'$ file = file_exists($ imageGrabTail-> getUploadDir()。'/ 19.png');'並且它返回true –

相關問題