【问题标题】:php download script not workingphp下载脚本不起作用
【发布时间】:2014-04-05 20:26:21
【问题描述】:

下面的 .php 脚本对我不起作用 - 它只会打印乱码:

<?php
    $path = $_SERVER['DOCUMENT_ROOT']."/rapcDemo/documents/";
    $fullPath = $path.$_GET['download_file'];

    if ($fd = fopen ($fullPath, "r")) {
        $fsize = filesize($fullPath);
        $path_parts = pathinfo($fullPath);
        $ext = strtolower($path_parts["extension"]);
        switch ($ext) {
            case "pdf":
                header("Content-type: application/pdf"); // add here more headers for diff. extensions
                header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
                break;
            default;
                header("Content-type: application/octet-stream");
                header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
        }
        header("Content-length: $fsize");
        header("Cache-control: private"); //use this to open files directly
        while(!feof($fd)) {
            $buffer = fread($fd, 2048);
            echo $buffer;
        }
    }
    fclose ($fd);
?>

任何想法我做错了什么?

【问题讨论】:

  • 您可能没有发送正确的内容类型。
  • 它正在打印返回的内容。当您说“胡言乱语”时,您是什么意思?例如,如果您尝试回显图像,您会得到看起来很乱的文字
  • 我正在尝试下载 .pdf 文档。它返回:
  • %PDF-1.4 %���� 22 0 obj endobj 44 0 obj /Filter/FlateDecode/ID[]/Index/22 36Info] 21 0 R/Length 101/Prev 69012/Root 23 0 R/Size 58/Type/XRef/W[1 2 1]>>流 h�bbdb`�$�� ��{@bA� V!�u D&lt;�A�ˀ�d �?�DH0n@BzL��HL�e`bdX �����?�ݯy�A endstream endobj startxref 0 %%EOF 57 0 obj &lt;&gt;stream h�b```g2aa����π����@q �����488@�]��������W����� ȓ��H ���3 endstream endobj
  • 我注意到当我输入“exit;”时在'fclose($fd);'之后声明,页面根本不会显示。是“出口”吗?命令真的很有必要(我是 php 新手)

标签: php download


【解决方案1】:

嗯...这是我使用的脚本。也许会有所帮助。

主下载脚本:

<?php
  $path = $_SERVER['DOCUMENT_ROOT']."/rapcDemo/documents/";
  $fullPath = $path.$_GET['download_file'];
  if(is_file($fullPath)) {
        # required for IE
        if (ini_get('zlib.output_compression')) {
            ini_set('zlib.output_compression', 'Off');
        }
        # get the file mime type using custom function
        $mime = ($try = get_mime($fullPath)) ? $try : "application/octet-stream";
        # set headers for download
        header('Pragma: public');  // required
        header('Expires: 0');  // no cache
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($fullPath)) . ' GMT');
        header('Cache-Control: private', false);
        header('Content-Type: ' . $mime);
        header('Content-Disposition: attachment; filename="' . $_GET['download_file']. '"');
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: ' . filesize($fullPath)); // provide file size
        header('Connection: close');
        readfile($fullPath);  // push it out
        exit();
  }
?>

这是我的 get_mime() 函数:

   <?php
        function get_mime($url = "") {
            if (empty($url))
                return FALSE;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, 1);
            curl_setopt($ch, CURLOPT_NOBODY, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $results = explode("\n", trim(curl_exec($ch)));
            foreach ($results as $line)
            if (strtok($line, ':') == 'Content-Type')
                return trim(explode(":", $line)[1]);
            return FALSE;
        }
  ?>

【讨论】:

    【解决方案2】:

    好的!我刚刚在网上找到并修改了一个 php 脚本,它就像一个魅力:)

    链接如下:

    http://phpsnips.com/579/PHP-download-script-for-Large-File-Downloads

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 2012-12-28
    • 2010-11-16
    • 2018-10-23
    • 1970-01-01
    • 2011-10-15
    相关资源
    最近更新 更多