2013-04-16 20 views
1

我發現這個http://knplabs.com/blog/give-your-projects-a-gaufrette如何使用Gaufrette將圖像上傳並存儲到S3中而不使用捆綁包?

,並提供了代碼示例是

<?php 

$amazon = new AmazonS3('myKey', 'mySecretKey', 'myToken'); 
$adapter = new Gaufrette\Adapter\AmazonS3($amazon, 'my_bucket'); 
$filesystem = new Gaufrette\Filesystem($adapter); 

if (! $filesystem->has('foo')) { 
    $filesystem->write('foo', 'Some content'); 
} 

echo $filesystem->read('foo'); 

這似乎並沒有被寫入圖像文件。

我還發現Gaufrette upload image and store in AmazonS3

但答案似乎傾向於使用Gaufrette束爲symfony1.2傾斜。

我沒有使用Symfony,所以我希望有一個很好的例子,我可以通過Gaufrette將圖像上傳到S3。

謝謝。

+0

而不是'有些內容',你可以把變量,代表你的形象。 你如何獲得圖像? 如果它是由用戶上傳的,您可以在$ _FILES中找到它,並使用提供的臨時文件名,您可以使用file_get_contents將其讀入到PHP變量中。 –

+0

抱歉。我實際上已經遠離了最後使用吹風機。我認爲你的答案是正確的。 –

回答

2

您的示例寫入文本「某些內容」。 而不是你可以把你的圖像內容。

$imageData = file_get_contents('/tmpupload/picture.jpeg'); 
$filesystem->write('foo', $imageData); 

您還應該將圖像的MIME類型寫入亞馬遜/ gaufrette。 如果我沒有弄錯,這應該工作:

$filesystem->write('foo', $imageData,array('content-type' = 'image/jpeg')); 
+2

對於0.2.x,你應該調用'$ filesystem-> getAdapter() - > setMetadata($ filename,array('ContentType'=> $ mimetype));'設置MIME類型。這必須在寫之前完成。 –

相關問題