Make WordPress Core

Opened 5 years ago

Last modified 5 years ago

#47049 new defect (bug)

Unzip_file causing Media file upload error

Reported by: troytempleman's profile troytempleman Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.1.1
Component: Upload Keywords:
Focuses: Cc:

Description

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 and I don't get the error when I remove the function.

https://codex.wordpress.org/Function_Reference/unzip_file

Change History (3)

#1 @mehrshaddarzi
5 years ago

I tested it.
Where do you want to use this code?
When uploading any file in WordPress? Or at the time of sending a form?
You need a WordPress hook. e.g.

add_action('add_attachment', function( $post_ID ){ 
$file_path = get_attached_file( $post_ID );
// Do unzip file
});

or

add_action('init', function() { if(isset($_FILE)) { ....

#2 @troytempleman
5 years ago

Hi Mehrshad,

Thanks for your help. Ideally I want it in the Customizer, after uploading a zip file of icon fonts. For example, in the Twenty Nineteen theme, in the inc/customizer.php file, I have the following control added to the twentynineteen_customize_register function:

$wp_customize->add_section( 'icon_fonts' , array(
    'title' => __( 'Icon Fonts', 'icon-fonts' ),
) );
	
$wp_customize->add_setting('icon_fonts', array(
    'default' => '',
));

$wp_customize->add_control( new WP_Customize_Upload_Control($wp_customize, 'icon_fonts', array(
    'label' => __('Icon Fonts', 'twentynineteen'),
    'section' => 'icon_fonts',
)));

This Customizer control can upload a zip file to the current uploads folder but when the unzip_file function above is added to functions.php, I'm unable to upload a zip file, or any other file to the Media library.

The function does work properly by unzipping the file if its already in the folder. The problem is not being able to upload when the function is being used.

Is there a better way of using it that will allow media uploads? I have tried customize_save and customize_save_after hooks, but then I get the following error when I click Publish in the Customizer:

Looks like something’s gone wrong. Wait a couple seconds, and then try again.

Not sure how to use your suggestions for this.

#3 @troytempleman
5 years ago

I tried the add_attachment hook below and the good news it works with no media upload errors, but the bad news this happens on every media upload, not just the icon fonts.

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' );

So, I tried restricting it to the icon fonts:

function unzip_icon_fonts( $post_ID ){
    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' );

However, this doesn't work at all. Am I missing something?

Note: See TracTickets for help on using tickets.