Support

Account

Home Forums General Issues Unzip folder on file upload Reply To: Unzip folder on file upload

  • 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
            $zip->extractTo($target);
    
            //Closing the zip file
            $zip->close();
    
            // Deleting the zip file
            unlink($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…