【问题标题】:PHP script downloads fine on Localhost but on live site outputs filePHP 脚本在 Localhost 上下载正常,但在实时站点输出文件上
【发布时间】:2013-05-25 07:17:02
【问题描述】:

请原谅我,我是 PHP 的处女,我正在为 wordpress 编写一个插件,允许某人向客户出售代码,然后该客户访问该站点并将该代码输入到文本框中,然后点击提交。然后 php 脚本检查 mysql 是否存在该代码,如果它存在,它将启动下载,因为它的销售下载(zip 中的照片)它会抓取服务器上给出的没有扩展名的文件名,然后将其输出为它应该在的文件名用于下载的另存为框,就像我完成代码的功能一样,我在第一次在本地测试后在实时站点上对其进行测试......现在这是本地主机(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;

希望你们能帮助或指出我正确的方向,也请解释一下,因为我是来学习而不是复制的!

问候, 亚当

【问题讨论】:

  • 我开始认为它可能与实时服务器上的内部服务器设置有关,如果我不知道它可能是什么...

标签: php wordpress file download output


【解决方案1】:

这可能是由多种原因造成的。两个最常见的原因是:

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

  2. 您的代码正在使用 php 短标签,而服务器已将它们关闭 <? vs <?php

【讨论】:

  • 这是 wordpress 中的一个插件,wordpress 是用 php 编写的,因此我无法想象这就是为什么脚本本身可以正常工作和运行的原因,但是当它在本地服务器上下载时在实时服务器上很好,它以文本形式在屏幕上输出文件,但所有 wordpress 模板都完好无损。
  • 附言。我正在使用完整的 php 标签
【解决方案2】:

检查服务器的内部设置并查看是否启用了 PHP。如果已启用,请尝试重新配置您的服务器和 php.ini。如果问题仍然存在,你应该在朋友的服务器上检查一下,看看你的服务器是否有问题。

【讨论】:

  • 再次如其他答案中所述,如果未启用 PHP,WORDPRESS 是用 php 构建的,那么 WORDPRESS 甚至无法工作!我的插件脚本的所有其他部分也不会,问题仅在于下载!
【解决方案3】:

好吧,这就是我修复它的方式...我必须将标题部分添加到单独的 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);
?>

然后开始下载的函数是这样的:

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>";
}

所以我从来没有深入了解它为什么会这样,但这提供了一个解决方案!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 2016-05-12
    • 2016-02-16
    • 2010-12-09
    • 2014-07-10
    • 2016-03-01
    相关资源
    最近更新 更多