What should I pass for $needed_dirs when calling _unzip_file_pclzip (aka PclZip)?
-
16-10-2019 - |
سؤال
I'm trying to create a fallback method when ZipArchive is not present. I'm seeking to use the _unzip_file_pclzip() function contained in wp-admin/includes/file.php
However, I'm not sure what is expected for the $needed_dirs argument.
My target folder for the zip will be the "styles" folder under my theme folder, so the path would be "wp-content/themes/mytheme/styles/"
Is that what's expected for $needed_dirs?
function openZip($file_to_open) {
global $wp_filesystem;//required if _unzip_file_pclzip is used directly
global $target;
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
{
$needed_dirs = array();
_unzip_file_pclzip($file_to_open, $target, $needed_dirs);
}
}
المحلول
@Scott B I have not tested this script, taken from inlcudes/file.php -> line 559
_unzip_file_pclzip
assumes WP_Filesystem() has already been called, so you need to set up global $wp_filesystem
global $wp_filesystem;
$needed_dirs = array();
$target = trailingslashit($target);
// Determine any parent dir's needed (of the upgrade directory)
if ( ! $wp_filesystem->is_dir($target) ) { //Only do parents if no children exist
$path = preg_split('![/\\\]!', untrailingslashit($target));
for ( $i = count($path); $i >= 0; $i-- ) {
if ( empty($path[$i]) )
continue;
$dir = implode('/', array_slice($path, 0, $i+1) );
if ( preg_match('!^[a-z]:$!i', $dir) ) // Skip it if it looks like a Windows Drive letter.
continue;
if ( ! $wp_filesystem->is_dir($dir) )
$needed_dirs[] = $dir;
else
break; // A folder exists, therefor, we dont need the check the levels below this
}
}
لا تنتمي إلى wordpress.stackexchange