2012-01-11 133 views
0

我有一個問題,使用Symfony上傳圖像。從鏈接獲取圖像(Symfony)

我有一個表單,我得到橫幅鏈接,這些橫幅是在不同的網站上託管。

但是,我需要將它們保存在我的服務器上,如何在Symfony的操作類中進行操作?

謝謝

回答

1

不要用動作,用一個表格!

您創建了一個簡單的文本輸入,但是使用了一個自定義驗證器來擴展sfValidatorFile(用於經典文件上傳)。這個驗證器返回一個sfValidatedFile,使用save()方法可以很安全並且很容易保存。

這是我自己的一個例子代碼:

<?php 

/** 
* myValidatorWebFile simule a file upload from a web url (ftp, http) 
* You must use the validation options of sfValidatorFile 
* 
* @package symfony 
* @subpackage validator 
* @author  dalexandre 
*/ 
class myValidatorWebFile extends sfValidatorFile 
{ 
    /** 
    * @see sfValidatorBase 
    */ 
    protected function configure($options = array(), $messages = array()) 
    { 
    parent::configure($options, $messages); 
    } 

    /** 
    * Fetch the file and put it under /tmp 
    * Then simulate a web upload and pass through sfValidatorFile 
    * 
    * @param url $value 
    * @return sfValidatedFile 
    */ 
    protected function doClean($value) 
    { 
    $file_content = file_get_contents($value); 
    if ($file_content) 
    { 
     $tmpfname = tempnam("/tmp", "SL"); 
     $handle = fopen($tmpfname, "w"); 
     fwrite($handle, $file_content); 
     fclose($handle); 

     $fake_upload_file = array(); 
     $fake_upload_file['tmp_name'] = $tmpfname; 
     $fake_upload_file['name']  = basename($value); 

     return parent::doClean($fake_upload_file); 
    } 
    else 
    { 
     throw new sfValidatorError($this, 'invalid'); 
    } 
    } 

    /** 
    * Fix a strange bug where the string was declared has empty... 
    */ 
    protected function isEmpty($value) 
    { 
    return empty ($value); 
    } 
}