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 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)