F3 框架:如何在用作方法参数的函数中使用 $f3 变量?
F3 Framework: How to use $f3 variables in functions which are used as method's arguments?
这很可能是因为我对 PHP 不是很了解,但这里是这样的:我正在使用 Fat-Free Framework 创建一个项目,现在我遇到了一个问题我'我有时间去 solve/understand。
这是在文件上传的回调方法中发生的,我正在使用 Fat-Free 的 Web 扩展处理,使用 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;
}
);
在此先感谢您对此的任何意见。
是的,我知道这是缺乏 PHP 知识。必须调用 Base 实例,以便可以访问变量。这意味着我们可以在这些函数中调用它,它将检索值:
$f3=Base::instance();
$maxFileSizeMb = $f3->get('maxFileSizeMb');
(来自类似问题的解决方案,认为该解决方案并不完全适用 - 至少我无法让所有替代方案都有效:
Fat-Free-Framework global variables and functions)
这很可能是因为我对 PHP 不是很了解,但这里是这样的:我正在使用 Fat-Free Framework 创建一个项目,现在我遇到了一个问题我'我有时间去 solve/understand。
这是在文件上传的回调方法中发生的,我正在使用 Fat-Free 的 Web 扩展处理,使用 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;
}
);
在此先感谢您对此的任何意见。
是的,我知道这是缺乏 PHP 知识。必须调用 Base 实例,以便可以访问变量。这意味着我们可以在这些函数中调用它,它将检索值:
$f3=Base::instance();
$maxFileSizeMb = $f3->get('maxFileSizeMb');
(来自类似问题的解决方案,认为该解决方案并不完全适用 - 至少我无法让所有替代方案都有效: Fat-Free-Framework global variables and functions)