2014-04-02 41 views
0

我已使用file upload example將文件添加到實體。現在我試圖從以前的項目中導入現有的文件(在WordPress中)。我已經編寫了一個腳本來導入Symfony2中的當前數據庫。從url導入文件而不是上傳

除文件上傳以外,一切進展順利。我想重命名文件(using the id as the filename)。該示例使用請求來處理表單,但由於我正在使用當前數據庫來獲取數據,因此沒有請求。

我已經試過這樣:

$old_image = '../../../public_html/uploads/images/'.$filename; 

$file = new UploadedFile($old_image, '337-1.jpg'); 

$img = new Entity\Image(); 
$img->setFile($file); 

$this->em->persist($img); 

我得到這個錯誤:

The file "337-1.jpg" was not uploaded due to an unknown error.

我怎樣才能設置一個文件,而無需使用一種形式(和要求)?

+0

你在哪兒,實際上上傳的文件複製到最終的目錄結構的邏輯是什麼? –

+0

@AlbertoFernández沒有「上傳」文件。它已經在文件系統中。我使用這個例子:http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html#using-the-id-as-the-filename –

+0

那麼,爲什麼你使用'UploadedFile'?已經有上傳的文件?你可以發佈剩下的實體結構嗎? –

回答

1

正如評論中所建議的HttpFoundation/File實體應該更適合您的用例。另外,請確保您的Image::setFile()方法來更改簽名:

public function setFile(File $file) 

到:

public function setFile($file) 
相關問題