$ wp_filesystem返回null。依赖性是什么?
-
16-10-2019 - |
题
我需要获得对$ WP_FILESYSTEM对象的引用。在下面的测试中,var_dump($ wp_filesystem)返回null。为了正确设置$ wp_filesystem,需要哪些其他文件?
我期望,由于将其调用为file.php,因此加载该文件就足以加载对象。
<?php
require('../../../wp-blog-header.php');
require('../../../wp-admin/includes/file.php');
$mytest = somefunction();
function somefunction() {
global $wp_filesystem;
var_dump($wp_filesystem);
return;
}
?>
更新:我发现我可以直接调用WP_FILESYSTEM()来创建它,因此我要提取的Zip正常,现在问题是将ZIP文件复制到目标文件夹,而不是像我的Ziparchive方法一样删除。
require('../../../wp-blog-header.php');
require('../../../wp-admin/includes/file.php');
function openZip($file_to_open) {
global $target;
global $wp_filesystem;
if(class_exists('ZipArchive'))
{
$zip = new ZipArchive();
$x = $zip->open($file_to_open);
if($x === true)
{
$zip->extractTo($target);
$zip->close();
unlink($file_to_open);
} else {
die("There was a problem. Please try again!");
}
}
else
{
WP_Filesystem();
$my_dirs = ''; //What should this be? I'm already passing he $target directory
_unzip_file_pclzip($file_to_open, $target, $my_dirs);
}
}
解决方案
$wp_filesystem
是一个全局变量,包含在运行文件系统“工厂”之后(自动)配置的文件系统对象的实例。
要运行工厂“跨越”全局变量(为了设置),只需调用 WP_Filesystem()
功能是,猜猜是什么, 在法典中无证. 。至少是 DocBlock包含一些信息 您可以读取Sourcecode(如果是您的选择)。
无论如何,我会尝试将函数调用添加到您的代码中,然后在需要file.php中 /wp-admin/includes
目录。可能这已经解决了您的问题。
如果您正在寻找(更好的)文件系统抽象,而不是内置的文件,请考虑 SPL中的文件系统对象 这是PHP的一部分。