admin管理员组文章数量:1315958
When I attempt to use the unzip_file function below in my functions.php
:
require_once(ABSPATH . 'wp-admin/includes/file.php');
WP_Filesystem();
$destination = wp_upload_dir();
$destination_path = $destination['path'];
$unzipfile = unzip_file( $destination_path.'/filename.zip', $destination_path);
if ( is_wp_error( $unzipfile ) ) {
echo 'There was an error unzipping the file.';
} else {
echo 'Successfully unzipped the file!';
}
I get the following error when trying to upload any files into the Media library in Media > Add New:
An error occurred in the upload. Please try again later.
I am using Twenty Nineteen theme with no plugins activated. I don't get the error when I remove the function.
Any ideas?
When I attempt to use the unzip_file function below in my functions.php
:
require_once(ABSPATH . 'wp-admin/includes/file.php');
WP_Filesystem();
$destination = wp_upload_dir();
$destination_path = $destination['path'];
$unzipfile = unzip_file( $destination_path.'/filename.zip', $destination_path);
if ( is_wp_error( $unzipfile ) ) {
echo 'There was an error unzipping the file.';
} else {
echo 'Successfully unzipped the file!';
}
I get the following error when trying to upload any files into the Media library in Media > Add New:
An error occurred in the upload. Please try again later.
I am using Twenty Nineteen theme with no plugins activated. I don't get the error when I remove the function.
Any ideas?
Share Improve this question asked Apr 26, 2019 at 0:57 Troy TemplemanTroy Templeman 4217 silver badges24 bronze badges 4 |2 Answers
Reset to default 0I got it to work without errors by using it inside add_attachment
hook below, but now the problem is it runs on every media upload.
function unzip_icon_fonts( $post_ID ){
$file_path = get_attached_file( $post_ID );
require_once(ABSPATH . 'wp-admin/includes/file.php');
WP_Filesystem();
$destination = wp_upload_dir();
$destination_path = $destination['path'];
$unzipfile = unzip_file($file_path, $destination_path);
}
add_action( 'add_attachment', 'unzip_icon_fonts' );
I tried restricting it to a specific Customizer file upload below, but then it doesn't work at all.
function unzip_icon_fonts( $post_ID ){
// If attachment ID equals Customizer file upload ID added with WP_Customize_Media_Control
if ( $post_ID == get_theme_mod('icon_fonts', '') ) {
$file_path = get_attached_file( $post_ID );
require_once(ABSPATH . 'wp-admin/includes/file.php');
WP_Filesystem();
$destination = wp_upload_dir();
$destination_path = $destination['path'];
$unzipfile = unzip_file($file_path, $destination_path);
}
}
add_action( 'add_attachment', 'unzip_icon_fonts' );
Am I missing something?
I know this might be a little old, but after you get_attached_file, you can check the path for the extension (https://www.php/manual/en/function.pathinfo.php):
<?php
$file_path = get_attached_file( $post_ID );
$path_parts = pathinfo($file_path);
// do nothing if not zip
if ( path_parts['extension'] !== 'zip' ) {
return;
}
require_once(ABSPATH . 'wp-admin/includes/file.php');
WP_Filesystem();
$destination = wp_upload_dir();
$destination_path = $destination['path'];
$unzipfile = unzip_file($file_path, $destination_path);
本文标签: phpUnzipfile causing Media file upload error
版权声明:本文标题:php - Unzip_file causing Media file upload error 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741974340a2408030.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
functions.php
file? – Sally CJ Commented Apr 26, 2019 at 3:09functions.php
exactly as above. There is afilename.zip
file in my uploads folder, which gets unzipped into the same folder as it should. The only problem is uploading media files while this function is in place. – Troy Templeman Commented Apr 26, 2019 at 10:14init
? E.g.add_action( 'init', function(){ if ( expression ) { // run your code } } );
– Sally CJ Commented Apr 26, 2019 at 16:13