Batch Export User Photos from WordPress and Compress Them into a Zip File for Download

We recently helped a high school build a student registration management system. After the student data was imported, the system created users automatically and let students or their families upload the required information and photos.

Collecting the data was only the first step. The real value came from organizing and exporting it. For example, school staff needed to calculate uniform sizes and export related student information quickly instead of checking everything manually.

One of the supporting features in that system was the ability to export uploaded user photos in bulk as a Zip file. The implementation itself was quite simple.

Export uploaded user photos in bulk as a Zip archive and trigger a download

The basic flow is to create a temporary export directory, copy the user photos into that directory with whatever naming rules you want, then compress the directory into a Zip file and return that archive as a download.

/**
 * Export uploaded user content.
 */
new Dispatch( [
    'export/([a-z]*)' => function ( $request, $status = 'fenxiang' ) {
        $dir     = wp_upload_dir()['basedir'] . '/export';
        $archive = wp_upload_dir()['basedir'] . '/archive.zip';

        if ( ! is_dir( $dir ) ) {
            mkdir( $dir, 0755, true );
        }

        // Copy matching photos into the export directory.
        // Rename files if needed.
        // Build the zip archive and return it to the user.
    },
] );

In a real project you would usually add a few more safety checks. For example, skip missing photos so the export process does not fail, rename files consistently, or export additional fields alongside the photos.

Although this example was implemented inside WordPress, the same overall export pattern can be used in many other systems as well.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *