这很可能是因为我对PHP不是很了解,但是这里说:我使用无脂框架来创建一个项目,而且我现在碰到了一个问题。我有一段时间来解决/理解。
这发生在文件上传的回调方法中,我使用receive($func=NULL,$overwrite=FALSE,$slug=TRUE)
方法处理无Fat的 Web 扩展名(两者都是$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;
}
);
提前感谢您对此的任何意见。
答案 0 :(得分:1)
是的,我知道缺少PHP知识。调用Base实例以便可以访问变量是必要的。这意味着我们可以在这些函数中调用它,它将检索值:
$f3=Base::instance();
$maxFileSizeMb = $f3->get('maxFileSizeMb');
(来自类似问题的解决方案,认为解决方案并非完全适用 - 至少我无法使所有替代方案发挥作用: Fat-Free-Framework global variables and functions)