Support

Account

Home Forums ACF PRO Zip download of all .vcard files from File field

Solving

Zip download of all .vcard files from File field

  • So here’s a very specific question. I have a user meta field where members can upload a vcard via an ACF File field. I would like to dynamically package all the vcards in a zip for users to download. I came across this code for creating a dynamic zip for gallery field images, but couldn’t adapt to 1. File field and 1. User meta data. Any help is greatly appreciated. Thanks!

  • In the code you gave as and example, all of the images are stored in a single field. You’re difference is that the values are stored in a field associated with each user.

    First you need to query all of the users https://codex.wordpress.org/Function_Reference/get_users

    then you need to loop through the list of users and get the value of the field for each and from this get the file path and add it.

  • It is helpful to know to run the user query first, but my main question is how to take the file field value and create a zip from that. I understand if this is outside of the scope of support here, but if you do know of any helpful links for the create zip side of the question I would greatly appreciate it!

  • I don’t see where this $files[] = get_attached_file( $singlefile['ID'] ); would be any different. It gets the attachments file location path from the ID of the file. As long as your returning either the File ID, or File Array so that you can get the ID, there should be no difference from using image fields except that gathering the list of files will be more complicated as I said above.

  • I think I am getting closer. The code below is returning the file paths but not creating the zip. Should I be using get_attached_file here?

    <?php $files_to_zip = array(); // create array
    $blogusers = get_users();
    $zip = new ZipArchive;
    $zip->open('file.zip', ZipArchive::CREATE);
    foreach ( $blogusers as $user ) { 
    $zip->addFile(the_field('upload_vcard', 'user_'. $user->ID )); }
    
    print_r($files_to_zip);// debug - return file names OK
    
    $zip->close();
    ?>
  • First is that the_field() echos the field, that’s not what you want. You need to use get_field(). Then you need to get the path to the actual file. ACF only stores the attachment ID and will return what you set it to return. You need to take what is returned and then get the actual full file path to the file and use this file path to add it to the .zip file, like the code in the example your following does.

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

The topic ‘Zip download of all .vcard files from File field’ is closed to new replies.