php下载脚本无效

时间:2014-04-05 20:26:21

标签: php download

下面的.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);
?>

任何想法我做错了什么?

2 个答案:

答案 0 :(得分: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;
        }
  ?>

答案 1 :(得分:0)

确定!我刚刚在网上找到并修改了一个像脚本一样的PHP脚本:)

链接如下:

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