Export media files to a zip archive

Export media files to a zip archive

Wordpress

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:

<?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.

FAQ

Common questions about WordPress project work and ongoing support.

What kinds of WordPress sites do you work with?

I work with business WordPress sites, WooCommerce stores, and projects that need to grow step by step — without unnecessary complexity or fragile solutions. This includes both new launches and existing sites that need refinements, support, or careful technical updates.

Can you improve an existing site without a full redesign?

Yes. Most tasks involve sites that are already live: new sections, functional improvements, WooCommerce changes, performance optimisation, or technical fixes. The goal is for the site to stay stable, manageable, and easy to develop further.

What types of tasks do you take on most often?

Most often this is custom WordPress development, Figma design implementation, WooCommerce refinements, support for live sites, fixing technical issues, and practical improvements over time. In selected scenarios I also help with light AI automations for content, enquiries, or internal admin workflows.

Do you work on WordPress speed and performance?

Yes. WordPress optimisation usually involves reviewing the theme, plugins, images, fonts, page structure, and the site's overall technical neatness. The aim is not a "magic button", but practical changes that make the site faster and easier to maintain.

Do you use AI when working on WordPress sites?

Yes, but only where it genuinely helps the process. AI works best for content drafts, FAQ or support assistants, enquiry handling, and small automations around an existing WordPress site. The approach is simple: AI should reduce routine work, not complicate the site.

What do I need to prepare to discuss a task or project?

Usually a short description of the site, the task, the desired outcome, and examples or technical constraints if you have them is enough. If the project is already live, it also helps to share the current site or a staging environment — this makes it easier to understand the scope of work.

Can I get in touch not for a new site, but for support or specific improvements?

Yes. I work not only on new builds, but also on live sites that need to be maintained, fixed, and improved gradually. This can include new pages, admin changes, WooCommerce refinements, small UX improvements, or technical maintenance.

Need help with a WordPress project?

If you need custom WordPress development, design implementation, or support for an existing site, I would be happy to review the project.