There is a lot of information on how to export media files to a zip archive on wordpress. There are a bunch of ready-made plugins that will work and solve most of the tasks. But when there is a non-trivial task, then you need to either finish something ready, or make your own.
Suppose you have a wordpress website, and your website users can upload a file / document to their personal account. There can be both an image and doc, docx, in general, everything that is not prohibited by the security system, or by you personally. The implementation of loading media files done using ACF. Files are uploaded to the uploads folder without any division by months / years, etc. And you need to download these files / documents from the site as an archive. The name of each file corresponds to the name of the one who uploaded it.
So, the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | <?php if (isset($_GET['archivemedia'])) { // archivemedia - get-request on which the function will be triggered if (current_user_can('manage_options')) { // check if the user is an administrator add_action('init', function () { // select users $args1 = array( 'role' => 'subscriber', 'order' => 'ASC', 'orderby' => 'display_name', 'fields' => 'all', ); $blogusers = get_users($args1); $path = 'uploads'; $file_folder = '/home/yourdomain.com/wp-content/uploads/'; // here you need to specify the full path to the folder with files $zip = new ZipArchive(); //connect library, in the majority of hostings it is supported. $time = time(); $zipArchive = $file_folder . $time . ".zip"; $zip_name = $file_folder . $time . ".zip"; $zipOutputName = $time . ".zip"; if ($zip->open($zip_name, ZIPARCHIVE::CREATE) === TRUE) { foreach ($blogusers as $user) { $meta = get_user_meta($user->ID); $id_image = (isset($meta['personal_user_upload_doc'][0]) && $meta['personal_user_upload_doc'][0] != '') ? $meta['personal_user_upload_doc'][0] : ''; //get the file data $user_name = (isset($meta['personal_user_name'][0]) && $meta['personal_user_name'][0] != '') ? $meta['personal_user_name'][0] : ''; // get username if ($id_image && !isset($id_image['error'])) { $parsed = parse_url(wp_get_attachment_url($id_image)); $url = rawurlencode(basename($parsed['path'])); $url_with_base = 'wp-content/uploads/' . $url; $pos = preg_replace("/.*?\./", '', $url); $fn = $pos; $filename = 'Doc_' . $user_name . '.' . $fn; //here we rename the file by assigning it a username $zip->addFile($url_with_base, $filename); } } $zip->close(); if (file_exists($zipArchive)) { header('Content-type: application/zip'); header('Content-Disposition: attachment; filename="' . $zipOutputName . '"'); readfile($zip_name); unlink($zip_name); } else { $error .= "* Files not found"; } } else { echo 'failed'; } }); } } |
As a result of this code execution, the archive with files should be downloaded on your PC.