将文件解压缩到$folder[\'path\']
您必须手动将所有解压缩文件添加到$\\u文件数组,并为其执行media\\u handle\\u上载等操作。您正在对其进行迭代,因此可以创建一个临时的$unzippedfiles数组,并将所有解压缩文件推入其中。您必须为每个文件创建一个类似于它在$\\u文件中显示的条目。尤其是文件应该放在默认的php上传目录中ini_get(\'upload_tmp_dir\')
以便wordpress函数能够正确处理它。
循环完整个$\\u FILES数组后,您可以用$unzippedfiles替换它,并在其上迭代执行附件内容。
以下是一些应该处理您的问题的代码:
$post_id = wp_insert_post($new_post);
if (!function_exists(\'wp_generate_attachment_metadata\')){
require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');
}
function dir_tree($dir) {
//http://www.php.net/manual/de/function.scandir.php#102505
$paths = array();
$stack[] = $dir;
while ($stack) {
$thisdir = array_pop($stack);
if ($dircont = scandir($thisdir)) {
$i=0;
while (isset($dircont[$i])) {
if ($dircont[$i] !== \'.\' && $dircont[$i] !== \'..\') {
$current_file = "{$thisdir}/{$dircont[$i]}";
if (is_file($current_file)) {
$paths[] = "{$thisdir}/{$dircont[$i]}";
} elseif (is_dir($current_file)) {
$paths[] = "{$thisdir}/{$dircont[$i]}";
$stack[] = $current_file;
}
}
$i++;
}
}
}
return $paths;
}
if ($_FILES) {
$unzipped = array();
foreach ($_FILES as $fid => $array) {
if ($array[\'error\'] !== UPLOAD_ERR_OK) {
return "upload error : " . $array[\'error\'];
}
WP_Filesystem();
$folder = ini_get(\'upload_tmp_dir\')."/unzipped_".basename($array[\'tmp_name\']);
if( ! is_dir( $folder ) ) {
mkdir( $folder );
}
if( unzip_file($array[\'tmp_name\'], $folder ) === True ) {
$filepaths = dir_tree( $folder );
foreach( $filepaths as $k => $filepath ) {
if( is_file($filepath) ) {
$file = array();
$file[\'name\'] = basename( $filepath );
$file[\'size\'] = filesize( $filepath );
$file[\'tmp_name\'] = $filepath;
$unzipped["unzipped_$k"] = $file;
}
}
}
$attach_id = media_handle_upload($fid, $post_id );
update_post_meta($post_id, \'_thumbnail_id\', $attach_id);
$post_id = wp_update_post();
}
foreach ($unzipped as $file ) {
$attach_id = media_handle_sideload($file,$post_id);
update_post_meta($post_id, \'_thumbnail_id\', $attach_id);
$post_id = wp_update_post();
}
}