WP未发送正确的标头来下载文件


WP not sending correct headers to download a file

我有一个WP插件,它发送指向电子邮件的链接以在隐藏位置下载文件。用户选择一个文件,然后将包含文件 ID 等的令牌写入数据库,并发送链接。单击链接(例如 mysite.com/thank-you/?id=asd687asd68as7d6a8sdd7sd768as7d6)后,"谢谢"页面会向 php 函数发出请求以加载文件。

还有一个虚拟的"下载引擎"页面,其中包含启动下载的短代码[download_page]。

奇怪的是,虽然这种设置在测试环境中运行良好,并且在生产现场工作了一年多,但它突然停止工作: 调用 DownloadFile() 返回 0KB 并在测试站点更正标头,并启动下载,但 返回完整的 HTML 文件,当然,没有下载。

我无法发现配置中的任何更改(两个站点都运行WP 4.2.7)。唯一的区别似乎是WooCommerce插件,以及包含在生产站点中的Google Analytics代码段。该测试站点运行PHP 5.3,而productiopn站点运行5.6。

如果有人对正在发生的事情有所了解,我将不胜感激。为什么标头会被忽略?(没有"标头已发送"错误。为什么显示虚拟"下载引擎"页面的完整 HTML?

顺便说一句,这是一个开箱即用的主题,但我无法发现其中的任何缺点。但是,也许..我去哪里找罪魁祸首?什么可以强制页面显示完整的 HTML 而不是 php 发送的标题?

相关代码如下:(下载管理器.php):

<?php 
  /*
  Plugin Name: Hidden Download Manager
  Description: Bla bla
  */
  include_once('add_meta_boxes.php');
  include_once('mfdm_frontend_functions.php');
  add_action( 'init', 'program_register' );
  add_shortcode( 'download_page', 'downloadFile' );
?>

(thank_you.tpl.php)

<?php
  /**
  Template Name: Thank You
  */
  $HOST = get_option('mfdm_dbhost');
  $USER = get_option('mfdm_dbuser');
  $PWORD = get_option('mfdm_dbpwd');
  $DB = get_option('mfdm_dbname');
  $connect_db = mysqli_connect($HOST,$USER,$PWORD,$DB) or die("Some error occurred during connection " . mysqli_error($connect_db));
  if(get_magic_quotes_gpc()) {
    $id = stripslashes($_GET['id']);
  } else {
    $id = $_GET['id'];
  }
  $result = $connect_db->query("SELECT * FROM 3_downloadkey WHERE uniqueid = '$id'");
  $row = resultToArray($result);
  $redirect = '';
  if (!$row) { 
    // redirect to 'invalid' page
    $redirect = get_option('mfdm_link_invalid_page');
    wp_redirect($redirect);
    exit;
  }
    get_header();
    $plugindir = plugin_dir_url(0).'mouseful_download/';
    // embed the javascript file that makes the AJAX request
    wp_enqueue_script( 'processLink_request', $plugindir . 'mf_downloads.js', array( 'jquery' ) );
    // declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
    wp_localize_script('processLink_request', 'ProcessLink', array(
      'ajaxurl' => admin_url('admin-ajax.php'),
      'postCommentNonce' => wp_create_nonce( 'ProcessLink-post-comment-nonce' ),
      'downloadHiddenPage' => get_option('mfdm_download_hidden_page')
    )
  );
?>
<link rel="stylesheet" type="text/css" media="all" href="<?=$plugindir;?>mf_downloads.css">

<body onLoad="setTimeout('downloadLink()', 1000)"></body>
<div class="contents about">
  <div class="row">
    <div class="col banner">
       <p><?php the_title(); ?></p>
    </div>
  </div>        
<div class="row2"><div class="col">
<?php
    while ( have_posts() ) : the_post();
        the_content();
    endwhile;
?>
</div></div></div>
<?php 
  get_footer(); 
?>

(mfdm_frontend_functions.php):

<?php
  add_action( 'wp_ajax_nopriv_downloadLink-submit', 'downloadFile' );
  add_action( 'wp_ajax_downloadLink-submit', 'downloadFile' );
function downloadFile() {
  $HOST = get_option('mfdm_dbhost');
  $USER = get_option('mfdm_dbuser');
  $PWORD = get_option('mfdm_dbpwd');
  $DB = get_option('mfdm_dbname');
  $id = $_POST['app'];
  $connect_db = mysqli_connect($HOST,$USER,$PWORD,$DB);
  $result = $connect_db->query("SELECT * FROM 3_downloadkey WHERE uniqueid = '$id'");
  $row = resultToArray($result);
  $app_ID = $row[0]['app_ID'];
  ob_start();
  // get app name 
  $base_dir = get_option('mfdm_applications_folder');
  define('BASE_DIR',$base_dir);
  $fname = get_post_meta($app_ID, 'meta_source', true);
  $app_download_name = get_post_meta($app_ID, 'meta_program_name', true);
  $file_path = '';
  $file_path = find_file1(BASE_DIR, $fname, $file_path);
  if (!is_file($file_path)) {
    die("File does not exist. Make sure you specified correct file name.");
  }
  $fsize = filesize($file_path); 
  $fext = strtolower(substr(strrchr($fname,"."),1));
  $mime_types = array(
    'msi' => 'application/x-ole-storage',
    'zip' => 'application/zip',
    'exe' => 'application/x-msdownload',
    'sh' => 'application/x-sh',
    'iso' => 'application/octet-stream',
    'dmg' => 'application/octet-stream',
    'pdf' => 'application/pdf',
  );
  $mm_type = $mime_types[$fext];
  if ($fd = fopen ($file_path, "r")) {
  $fsize = filesize($file_path);
  $path_parts = pathinfo($file_path);
  $ext = strtolower($path_parts["extension"]);
  header("Content-type: $mm_type");
  header("Content-Disposition: attachment; filename='"".$app_download_name."'"");
  header("Content-length: $fsize");
  header("Cache-control: private"); //use this to open files directly       
  while(ob_POST_level() > 0) {
    @ob_end_clean();
  }
  while(!feof($fd)) {
    $buffer = fread($fd, 2048);
    echo $buffer;
  }
}
fclose ($fd);
}
?>

(mf_downloads.js):

function downloadLink() {
  var id = window.location.search.replace("?", "").split("&")[0];
  id = id.split("=")[1];
  var url = ProcessLink.downloadHiddenPage;
  var form = jQuery('<form action="' + url + '" method="post">' + '<input type="text" name="app" value="' + id + '" hidden="hidden"/>' + '</form>');
  jQuery('body').append(form);
  form.submit();
} 

"mfdm_frontend_functions.php"中的"downloadFile"函数应以wp_die()结尾,以便返回正确的响应。

wp_die(); // this is required to terminate immediately and return a proper response

上传一个用户定义的 php.ini 到您的 webroot 包含以下行:

default_charset = ""