如何使用WordPress中的PHP headers()从自定义插件页面下载文件?
我试图使用WordPress中的PHP headers()从我的插件页面视图下载文件,但它不起作用,因为脚本中的每个标题行都会返回此错误消息: 警告:无法修改标题信息-第33行D:\xampp\wordpress\wp content\plugins\pxw test\pxw-test.php中的标题已经发送(输出从D:\xampp\wordpress\wp includes\formatting.php:5100开始) 对于当前标题的每一行,依此类推();在我的剧本里 您可以在下面查看pxw-test.php文件内容(插件本身):如何使用WordPress中的PHP headers()从自定义插件页面下载文件?,php,wordpress,plugins,Php,Wordpress,Plugins,我试图使用WordPress中的PHP headers()从我的插件页面视图下载文件,但它不起作用,因为脚本中的每个标题行都会返回此错误消息: 警告:无法修改标题信息-第33行D:\xampp\wordpress\wp content\plugins\pxw test\pxw-test.php中的标题已经发送(输出从D:\xampp\wordpress\wp includes\formatting.php:5100开始) 对于当前标题的每一行,依此类推();在我的剧本里 您可以在下面查看pxw-
下载文件
我的目标是下载一个文件,并在下载后删除该文件
当然我不是在找:
一个function.php解决方案,因为我正在构建一个插件
但我欢迎从我的插件文件夹中找到任何简短且易于管理的内容
例如:
,但不是
,因为我需要在下载后以及文件是否下载后执行删除文件等操作function pxw_test_dashboard_page(){
// You can not use header() here!
// For example when you include a PHP file here to display your page view,
// you have to separate you headers function in a external file
//then display the output in your file view.
}
因此,我找到的最好的解决方案,而不是许多麻烦黑客,是添加:
ob_start()
在PHP标记的开头和函数之外的某个地方。
以下是完整的工作代码:
插件文件夹:pxw测试
插件文件夹内容:
<?php
/*
Plugin Name: Test Headers
Description: Download File Test Plugin for Headers Response errors.
Version: 1.0
Author: Tester
Text Domain: pxw_test
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ){
exit;
}
// Fix PHP headers
ob_start();
// Set DB version
//global $pxw_test_db_version;
$pxw_test_db_version = '1.0';
// Admin menu
function pxw_test_menu() {
//create custom top-level menu
add_menu_page(
'Dashboard',
'Test Headers',
'manage_options',
'pxw_test',
'pxw_test_dashboard_page',
null,
20
);
}
// Function needed to display top-level page as the main page
function pxw_test_dashboard_page() {
$target_dir = plugin_dir_path( __FILE__);
// Download
if(isset($_POST['download'])){
// File To Download
$filename = 'file.txt';
// HTTP headers for downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($target_dir.$filename));
while (ob_get_level()) {
ob_end_clean();
@readfile($target_dir.$filename);
}
// Do something else after download like delete file
unlink($target_dir.$filename);
}
else{
}
?>
<form action="" method="POST" enctype="multipart/form-data" style="text-align:center;padding: 100px 0;">
<button type="submit" onclick="this.form.submit();" style="background:red;padding:10px;color:#fff;cursor:pointer;"><b>Download File</b></button>
<input type="hidden" name="download" />
</form>
<?php
} # End of function pxw_test_dashboard_page()
// Finally display the menu
add_action('admin_menu', 'pxw_test_menu');
?>
pxw test.php
<?php
/*
Plugin Name: Test Headers
Description: Download File Test Plugin for Headers Response errors.
Version: 1.0
Author: Tester
Text Domain: pxw_test
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ){
exit;
}
// Fix PHP headers
ob_start();
// Set DB version
//global $pxw_test_db_version;
$pxw_test_db_version = '1.0';
// Admin menu
function pxw_test_menu() {
//create custom top-level menu
add_menu_page(
'Dashboard',
'Test Headers',
'manage_options',
'pxw_test',
'pxw_test_dashboard_page',
null,
20
);
}
// Function needed to display top-level page as the main page
function pxw_test_dashboard_page() {
$target_dir = plugin_dir_path( __FILE__);
// Download
if(isset($_POST['download'])){
// File To Download
$filename = 'file.txt';
// HTTP headers for downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($target_dir.$filename));
while (ob_get_level()) {
ob_end_clean();
@readfile($target_dir.$filename);
}
// Do something else after download like delete file
unlink($target_dir.$filename);
}
else{
}
?>
<form action="" method="POST" enctype="multipart/form-data" style="text-align:center;padding: 100px 0;">
<button type="submit" onclick="this.form.submit();" style="background:red;padding:10px;color:#fff;cursor:pointer;"><b>Download File</b></button>
<input type="hidden" name="download" />
</form>
<?php
} # End of function pxw_test_dashboard_page()
// Finally display the menu
add_action('admin_menu', 'pxw_test_menu');
?>
下载文件
工作解决方案
我现在明白了,在函数内部,PHP头由函数启动,并在函数结束时停止
function pxw_test_dashboard_page(){
// You can not use header() here!
// For example when you include a PHP file here to display your page view,
// you have to separate you headers function in a external file
//then display the output in your file view.
}
因此,我找到的最好的解决方案,而不是许多麻烦黑客,是添加:
ob_start()
在PHP标记的开头和函数之外的某个地方。
以下是完整的工作代码:
插件文件夹:pxw测试
插件文件夹内容:
<?php
/*
Plugin Name: Test Headers
Description: Download File Test Plugin for Headers Response errors.
Version: 1.0
Author: Tester
Text Domain: pxw_test
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ){
exit;
}
// Fix PHP headers
ob_start();
// Set DB version
//global $pxw_test_db_version;
$pxw_test_db_version = '1.0';
// Admin menu
function pxw_test_menu() {
//create custom top-level menu
add_menu_page(
'Dashboard',
'Test Headers',
'manage_options',
'pxw_test',
'pxw_test_dashboard_page',
null,
20
);
}
// Function needed to display top-level page as the main page
function pxw_test_dashboard_page() {
$target_dir = plugin_dir_path( __FILE__);
// Download
if(isset($_POST['download'])){
// File To Download
$filename = 'file.txt';
// HTTP headers for downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($target_dir.$filename));
while (ob_get_level()) {
ob_end_clean();
@readfile($target_dir.$filename);
}
// Do something else after download like delete file
unlink($target_dir.$filename);
}
else{
}
?>
<form action="" method="POST" enctype="multipart/form-data" style="text-align:center;padding: 100px 0;">
<button type="submit" onclick="this.form.submit();" style="background:red;padding:10px;color:#fff;cursor:pointer;"><b>Download File</b></button>
<input type="hidden" name="download" />
</form>
<?php
} # End of function pxw_test_dashboard_page()
// Finally display the menu
add_action('admin_menu', 'pxw_test_menu');
?>
pxw test.php
<?php
/*
Plugin Name: Test Headers
Description: Download File Test Plugin for Headers Response errors.
Version: 1.0
Author: Tester
Text Domain: pxw_test
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ){
exit;
}
// Fix PHP headers
ob_start();
// Set DB version
//global $pxw_test_db_version;
$pxw_test_db_version = '1.0';
// Admin menu
function pxw_test_menu() {
//create custom top-level menu
add_menu_page(
'Dashboard',
'Test Headers',
'manage_options',
'pxw_test',
'pxw_test_dashboard_page',
null,
20
);
}
// Function needed to display top-level page as the main page
function pxw_test_dashboard_page() {
$target_dir = plugin_dir_path( __FILE__);
// Download
if(isset($_POST['download'])){
// File To Download
$filename = 'file.txt';
// HTTP headers for downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($target_dir.$filename));
while (ob_get_level()) {
ob_end_clean();
@readfile($target_dir.$filename);
}
// Do something else after download like delete file
unlink($target_dir.$filename);
}
else{
}
?>
<form action="" method="POST" enctype="multipart/form-data" style="text-align:center;padding: 100px 0;">
<button type="submit" onclick="this.form.submit();" style="background:red;padding:10px;color:#fff;cursor:pointer;"><b>Download File</b></button>
<input type="hidden" name="download" />
</form>
<?php
} # End of function pxw_test_dashboard_page()
// Finally display the menu
add_action('admin_menu', 'pxw_test_menu');
?>
下载文件
问题在于,您一开始就在错误的地方这样做。(注释//将顶级页面显示为主页所需的函数本身应该已经表明了这一点。这不是将完全不同的功能塞进其中的地方。)基本上,您应该在WP响应生成过程中寻找正确的操作挂钩,以生成下载文件所需的头文件和输出。根据这个答案模板\u重定向应该可以正常工作。@CBroe-谢谢你的回答。。我应该把模板放在哪里?你能分享一个基于我上面脚本的代码示例吗?我试过了,但没有成功。我已经检查了所有相关的帖子,包括你提到的那篇。。但是运气不好,问题是你一开始就在错误的地方做这件事。(注释//将顶级页面显示为主页所需的函数本身应该已经表明了这一点。这不是将完全不同的功能塞进其中的地方。)基本上,您应该在WP响应生成过程中寻找正确的操作挂钩,以生成下载文件所需的头文件和输出。根据这个答案模板\u重定向应该可以正常工作。@CBroe-谢谢你的回答。。我应该把模板放在哪里?你能分享一个基于我上面脚本的代码示例吗?我试过了,但没有成功。我已经检查了所有相关的帖子,包括你提到的那篇。。但是没有运气。。