2017-06-15 35 views
1

これは私がPHPに精通していないために起こりそうですが、ここでは:私はFat-Free Frameworkを使ってプロジェクトを作成していますが、今は問題にぶつかりました。 /わかる。

これは$func$slug両方が私が使用している機能を、することができ(receive($func=NULL,$overwrite=FALSE,$slug=TRUE)メソッドを使用して、私は無脂肪のウェブ拡張子の取り扱いだファイルアップロードのためのコールバックメソッドの内部で何が起こっています以下の例を参照)。この拡張では、何らかの方法でファイルを検証するための引数として関数を使用し、ファイル名を変更するために関数を使用できます。

問題は、これらのメソッド内でグローバルな$ f3変数を使用できないことです。例えば。下のコードでは、maxFileSizeMb変数に最大ファイルサイズを確認する変数を設定したいと考えていますが、$this->f3->get('maxFileSizeMb')を直接呼び出すか、関数の前の変数に代入してコードを破棄します。

$this->f3->set('UPLOADS','uploads/'.$this->f3->get('tmpMediaPath').'/'); 
$this->f3->set('maxFileSizeMb', 2); 
$this->f3->set('fileNameLenght', 30); 

// Using f3 \Web extension 
$overwrite = false; // set to true, to overwrite an existing file; Default: false 
// $slug = true; // we'll generate a new filename 
$web = \Web::instance(); 
$files = $web->receive(function($file,$formFieldName) { 

     // Check against the maximum allowed file size 
     if($file['size'] > (2 * 1024 * 1024)) // if bigger than 2 MB 
     //    >>>^<<< using $this->f3->get('maxFileSizeMb'); breaks the code 

      return false; // this file is not valid, return false will skip moving it 

     return true; // allows the file to be moved from php tmp dir to your defined upload dir 
    }, 
    $overwrite, 
    function($fileBaseName, $formFieldName){ 

     $fileExtension = ".tmp"; // Determine the true image type and rename it later on 
     // ##TODO## check if value is truly unique against the database. Here or elsewhere? 
     $randomName = bin2hex(openssl_random_pseudo_bytes(30)); 
     //           >>> ^^ <<< using $this->f3->get('fileNameLenght'); breaks the code 
     return $randomName.$fileExtension; 
    } 
); 

ありがとうございます。

答えて

1

ええ、私はこれがPHP知識の欠如であることを知っていました。変数がアクセス可能になるようにBaseインスタンスを呼び出す必要があります。少なくとも、私はすべての作ることができなかった - ソリューションは、完全には適用されないと思った、同様の質問から

$f3=Base::instance(); $maxFileSizeMb = $f3->get('maxFileSizeMb');

(解決策:これは、我々は、これらの機能の中に、これを呼び出すことができますし、それが値を取得することを意味します代替案は、 Fat-Free-Framework global variables and functions

関連する問題