Home Forums General Issues Unzip folder on file upload


Unzip folder on file upload

  • what is the best way to unzip a folder during the upload process?

    I am using ACF for a front end form which allows files to be uploaded. In some cases a Zip file will need to be uploaded and I have been trying to find a way to unzip the file before saving the folder somewhere.

    I have tried the below code but am struggling to get this working and need to somehow get the new designation URL in the FILES array.

    Can anyone help?

    //this ACF hook is used to update a value, we need to unzip the file and update the destination
    function unzip_powerpoint_file( $value, $post_id, $field  ) {
    	//unzip file
    	 global $wp_filesystem;
    	$destination = wp_upload_dir();
    	$destination_path = $destination['path'];
    	$unzipfile = unzip_file( $destination_path.'/', $destination_path);
    	if ( !$unzipfile ) {
    		echo 'There was an error unzipping the file.';       
    	// override value
        update_attached_file( $value, $unzipfile );
    	// return
        //return $value;
    add_filter('acf/update_value/name=powerpoint', 'unzip_powerpoint_file', 10, 3);
  • Hi @leanne-oleary

    I think you need to use the acf/save_post hook instead. Also, please keep in mind that unzip_file() will only return true on success, so this code:

    update_attached_file( $value, $unzipfile );

    Won’t be working. For further support regarding unzipping files in WordPress, please consult to WordPress Support forum instead.

    I hope this helps.

  • Many thanks for your help 🙂

  • I cannot get it to work… tried it with the acf/save_post hook but the file does not get unzipped, I am using a custom directory for the uploads field. this is what I have:

     * Adding special uploads folder for VR files
    add_filter( 'acf/upload_prefilter/name=vr_files_upload', 'vr_files_upload_prefilter' );
    add_filter( 'acf/prepare_field/name=vr_files_upload', 'vr_files_upload_field_display' );
    function vr_files_upload_prefilter( $errors ) {
      add_filter( 'upload_dir', 'vr_files_upload_directory' );
      return $errors;
    function vr_files_upload_directory( $uploads ) {
      $folder = '/vr-files';
      $uploads['path'] = $uploads['basedir'] . $folder;
      $uploads['url'] = $uploads['baseurl'] . $folder;
      $uploads['subdir'] = '/';
      return $uploads;
    function vr_files_upload_field_display( $field ) {
      // update paths accordingly before displaying link to file
      add_filter( 'upload_dir', 'vr_files_upload_directory' );
      return $field;

    I have tried the WP function unzip_file in a few different ways but that didn’t work. Then I tried the PHP zip functions… still not working…

     * @file – path to zip file
     * @target – destination directory for unzipped files
    function unzip_vr_file(){
    	$file = get_field('vr_files_upload');
    	$target = pathinfo( get_attached_file( $attachment_id ) );
    	// Creating new ZipArchive object
        $zip = new ZipArchive();
        // Opening the file
        $open = $zip->open($file);
        //Checking if file has been opened properly
        if($open === true) {
            // Extracting the zip file
            //Closing the zip file
            // Deleting the zip file
            return true;
        else {
            return false;
    add_filter('acf/save_post', 'unzip_vr_file', 10, 3);

    Can anybody give me a hint into the right direction? Wasted hours now for finding the correct way of doing this. Would be great to have this in the ACF documentation as there are probably several people wondering the same thing…

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Unzip folder on file upload’ is closed to new replies.