douyingbei1458 2017-05-12 03:13
浏览 52

在wp-admin文件夹中WordPress AJAX调用表现的结果

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;
              if(file_exists($_SERVER['DOCUMENT_ROOT'].'/bcg/wp-content/uploads/'.$path)){
                  $zip->addFromString(basename($base . $path),  file_get_contents($base . $path)); 
              }
              else {
               echo"file does not exist";
            }
        }
    }

    $zip_file_path = $zip_name;
    $zip->close();
    echo admin_url(). '/' . $zip_name;
    unlink($zip);
    wp_die();
}

add_action( 'wp_ajax_nopriv_download_folder', 'bcg_zip_download' );
add_action( 'wp_ajax_download_folder', 'bcg_zip_download' );
  • 写回答

2条回答 默认 最新

  • doubo82706 2017-05-12 04:51
    关注

    Replace

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

    to

    echo site_url();.'/zip-folder/'.$zip_name;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大