• I am trying to Zip up to 5 files and save them in an archive for each post. When I, for example, have 3 files and add another file the zip file will include the 3 files after I click on update. When I click again on update I then will have 4 files in my zip file. I cannot really figure out how to get it on the first click.

    My guess is that the acf fields are updated last on update and that is why I get the old values instead of the new ones. Because I delete the complete zip file and then recreate it, which means that the function creates the exact same zip file as before, even though it should’ve been changed.

    So I have tried to use different hooks: save_post, save_post_post, acf/save_post and publish_post. I got the same results no matter what. I also tried different priorities on each, 0,1, 999 and 99999. Still the same results.

    I also tried getting the field values with $_POST[‘acf’] and get_fields. With $_POST[‘acf’] nothing works, I cannot even delete the zip file when using the post variable. I looked at all the data which I need for the zipping and deleting process and it is the same as my other solution.

    In the following code I commented the code snippets that do not work at all and replaced it with the code snippets that do work but only create the zip I want when I click on update a second time.

    I also tried adding the hook before the start of the function and after the function. Didnt seem to have an effect at all.

    I know that deleting the complete zip file and reacreating it completly isnt the best practice, I also had a nice code snippet that would only delete the files that werent there anymore, but I dont know how many files will be needed, so it could be that there needs to be a custom fieldgroup for each post, with 20 files to upload or 2 files and I thought when I delete everything first it doesnt matter.

    add_action('acf/save_post' , 'archive_upload', 10, 3);
    
    function archive_upload($post_ID){
    
    $destination = 'wp-content/uploads/archiv/'. hash('md5', $post_ID).'.zip';
    
    $zip = new ZipArchive();
    $zip->open( $destination, ZipArchive::CREATE );
    
    $fields = get_fields($post_ID);
    //$fields = $_POST['acf'];
    if($fields != ''){
        $fieldslength = count($fields); 
        $i = 0;
        while( $i < $fieldslength )
        {               
            $zip->deleteIndex($i);
    
            $i++;
        }
        $zip->close();
    
        $zip = new ZipArchive();
        $zip->open( $destination, ZipArchive::CREATE );
    
        if( count( $fields ) ) {
    
            $i = 0;
            foreach( $fields as $field ) {
    
                    $info .= " ".$field;
    
                    /*if($field){
                        $key = key($fields);
                        $upload = get_field($key);
                        $new_filename = $upload['filename'];
                        $url = get_attached_file($upload['id']);
                        $info .= " ".$key." ".$url." ".$new_filename;
                        $zip->addFile( $url, $new_filename );
    
                        $i++;
                    }
                    next($fields);*/
                        $new_filename = $field['filename'];
                        $url = get_attached_file($field['id']);
                        $info .= " ".$url." ".$new_filename;                    
                        $zip->addFile( $url, $new_filename );
                    }
            }
            //die($info);
    
        }   
    }
    $zip->close();
    
    }
    /*function custom_hook() {
        do_action('custom_hook');
    }
    add_action('custom_hook', 'archive_upload');*/
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘ZipArchive with ACF doesnt work correctly’ is closed to new replies.