2012-01-11 8 views
0

Symfonyを使用して画像をアップロードする際に問題があります。リンクから画像を取得する(symfony)

私はバナーのリンクを取得するフォームを持っています。これらのバナーは異なるWebサイトでホストされています。

しかし、私はどのように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); 
    } 
} 
関連する問題