這很可能是因爲我對PHP不太瞭解,但是這裏是:我使用無脂框架來創建一個項目,而現在我遇到了一個問題,我有一段時間需要解決/理解。
這是一個文件上傳,我要和無脂肪的網絡擴展處理回調方法裏面發生的事情,用receive($func=NULL,$overwrite=FALSE,$slug=TRUE)
方法(包括$func
和$slug
可以是函數,我使用的這下面的例子)。有了這個擴展,我可以使用一個函數作爲參數以某種方式驗證文件,另一個文件可以更改文件名。
問題是我不能在這些方法中使用任何全局$ 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;
}
);
在此先感謝您的任何意見。