[英]Result of WordPress AJAX Call Manifesting in wp-admin folder
I have a form in which users can select files and create a zip archive of the selected. 我有一个表单,用户可以在其中选择文件并创建所选文件的zip存档。 Everything is working as expected except for the fact that the zip file is created in the wp-admin folder.
除了zip文件是在wp-admin文件夹中创建的事实以外,其他所有操作均按预期进行。 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: 这是用于创建zip归档文件的PHP代码:
// 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' );
Replace 更换
echo admin_url(). '/' . $zip_name;
to 至
echo site_url();.'/zip-folder/'.$zip_name;
Try changing these lines 尝试更改这些行
$zip_name = time().".zip"; // Zip name
$zip_output_path = get_home_path().'tmp/'.$zip_name; //tmp folder in wordpress root path //rewrite to your need
if (!file_exists(get_home_path().'tmp/')) {
mkdir(get_home_path().'tmp/', 0777, true);
}
$zip->open($zip_output_path , ZipArchive::CREATE);
To delete the files, you may need to write another function to delete them right before you download them. 要删除文件,您可能需要在下载文件之前立即编写其他功能来删除它们。 REF http://php.net/manual/en/function.readfile.php OR write a cron to delete them periodically if that suits your application.
REF http://php.net/manual/zh/function.readfile.php或编写cron以适合您的应用程序定期删除它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.