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

时间:2016-03-13 15:32:23

标签: php wordpress

我有一个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发送的标题?

相关代码是这样的: (下载manager.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();
} 

1 个答案:

答案 0 :(得分:0)

'mfdm_frontend_functions.php'中的'downloadFile'函数应以wp_die()结尾,以便返回proper response

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

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

default_charset = ""