I have a form in which users can select files and create a zip archive of the selected. Everything is working as expected except for the fact that the zip file is created in the wp-admin folder. Instead, I would like for it to be housed in a temp folder and deleted immediately after it has finished downloading.

Here is the PHP code for the creation of the zip archive:

// Creates zip file
function bcg_zip_download() {

    $files = $_POST['checked'];

    $zip = new ZipArchive();
    $zip_name = time().".zip"; // Zip name
    $zip->open($zip_name,  ZipArchive::CREATE);
    $full = wp_upload_dir();
    $base = $full['baseurl'] .'/';

    if (is_array($files)){
        foreach ($files as $file) {
              $path = $file;
                  $zip->addFromString(basename($base . $path),  file_get_contents($base . $path)); 
              else {
               echo"file does not exist";

    $zip_file_path = $zip_name;
    echo admin_url(). '/' . $zip_name;

add_action( 'wp_ajax_nopriv_download_folder', 'bcg_zip_download' );
add_action( 'wp_ajax_download_folder', 'bcg_zip_download' );

Read more here: Result of WordPress AJAX Call Manifesting in wp-admin folder


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: