PHP脚本在Localhost上下载,但在实时站点输出文件上下载

时间:2013-05-25 07:17:03

标签: php wordpress file download output

请原谅我,我是PHP的完整处女,我正在为wordpress编写一个插件,允许某人向客户销售代码,然后让该客户访问该网站并将该代码输入到文本框中点击提交。 php脚本然后检查mysql是否存在该代码它是否会启动下载,因为它的销售下载(zip中的照片)它抓取在没有扩展名的服务器上给出的文件名然后输出它应该是的文件名保存为下载框,就像我完成代码的功能,然后在第一次测试本地后在现场网站上测试它...现在这是localhost(xampp)上的问题,它开始下载并正常工作在现场网站上它做到了这一点:

http://www.ctwo12.com/output.png

这是我的代码:

$fileonS = $_SERVER['DOCUMENT_ROOT'] . "/wp-content/plugins/photo_dwn_man/downloads/" .    $codeRResult;

//download file (NEEDS MORE LOOKING INTO THIS IS JUST THE BASICS)
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $codeOResult . '.zip');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($fileonS));
ob_clean();
flush();
readfile($fileonS);
exit;

希望你们可以帮助或指出我正确的方向,请解释,因为我在这里学习不复制!

的问候, 亚当

3 个答案:

答案 0 :(得分:0)

这可能是由许多事情引起的。两个最常见的原因是:

  1. 您的服务器上未启用PHP

  2. 您的代码使用的是php短标记,服务器已将其关闭<? vs <?php

答案 1 :(得分:0)

检查服务器的内部设置&amp;看看PHP是否已启用。如果已启用,请尝试重新配置您的服务器&amp; PHP。如果问题仍然存在,那么您应该在某个朋友的服务器上检查这一点,看看问题是否与您的服务器有关。

答案 2 :(得分:0)

这是我修复它的方法...我必须将标题部分添加到单独的PHP文件中,当输入和提交正确的代码时,我调用了一些JavaScript来加载PHP并传递一些GET变量..

我的单独文件包含:

<?php
    $getcodeOResult = $_GET['gcor'];
    $getcodeRResult = $_GET['gcsr'];
    $fileonS = $_SERVER['DOCUMENT_ROOT'] . "/wp-content/plugins/photo_dwn_man/downloads/" . $getcodeRResult;
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . $getcodeOResult . '.zip');
    header('Content-Transfer-Encoding: binary');
    header('Pragma: public');
    header('Content-Length: ' . filesize($fileonS));
    ob_clean();
    flush();
    readfile($fileonS);
?>

<?php $getcodeOResult = $_GET['gcor']; $getcodeRResult = $_GET['gcsr']; $fileonS = $_SERVER['DOCUMENT_ROOT'] . "/wp-content/plugins/photo_dwn_man/downloads/" . $getcodeRResult; header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . $getcodeOResult . '.zip'); header('Content-Transfer-Encoding: binary'); header('Pragma: public'); header('Content-Length: ' . filesize($fileonS)); ob_clean(); flush(); readfile($fileonS); ?>

然后开始下载的功能如下:

function startDownload() {
    // This function handles the download start
    // Get filename you want user to download by getting the contents of dB row that matches the user input
    $theCodeOfile = $GLOBALS['wpdb']->get_col( "SELECT dwn_file FROM wp_photodwnman WHERE dwn_code='" . $GLOBALS['userCode'] . "'");

    // Get the actual servers filename by getting the contents of dB row that matches the user input
    $theCodeRfile = $GLOBALS['wpdb']->get_col( "SELECT dwn_pseu FROM wp_photodwnman WHERE dwn_code='" . $GLOBALS['userCode'] . "'");

    // join both results into a string and not an array
    $codeOResult = join("", $theCodeOfile);
    $codeRResult = join("", $theCodeRfile);
    $GLOBALS['wpdb']->query( "UPDATE wp_photodwnman SET dwn_count=dwn_count+1 WHERE dwn_code='" . $GLOBALS['userCode'] . "'");
    // adds to variable the location and filename
    echo "<script>window.onload = function(){window.location.href='http://ctwo12photography.co.uk/wp-content/plugins/photo_dwn_man/dwnload.php?gcor=" . $codeOResult . "&gcsr=" . $codeRResult . "'};   </script>";
}

所以我从来没有明白为什么它表现得这样,但这提供了一个解决方案!