PHP下载文件冻结了计算机

时间:2013-06-19 15:09:24

标签: php download

我想创建一个下载链接,我在网上找到了一个例子。我忘了我得到它的地方。

以下是代码:

<?php

// place this code inside a php file and call it f.e. "download.php"
//$path = $_SERVER['DOCUMENT_ROOT']."/path2file/"; // change the path to fit your websites document structure
$path = "music/";
$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;
    case "mp3":
    header("Content-type: audio/mpeg"); // add here more headers for diff. extensions
    header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
    break;
    case "wma":
    header("Content-type: audio/wma"); // add here more headers for diff. extensions
    header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
    break;
    case "ogg":
    header("Content-type: audio/ogg"); // 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);
}
exit;
// example: place this kind of link into the document where the file download is offered:
// <a href="download.php?download_file=some_file.pdf">Download here</a>
?>

问题是当我点击下载链接时,它可以工作。但几秒钟后,我的整台计算机冻结,我需要强行关机。

这个问题的原因是什么?是我的电脑故障还是代码?我正在使用WAMP Server 2.

更新:

我尝试使用readfile()代替fread(),但我仍然遇到同样的问题。以下是我的代码:

<?php
$directory_load = simplexml_load_file('conf/configuration.xml');
$path = $_SERVER['SERVER_ROOT'].$directory_load -> directories;
echo $path;
if($_GET['download_file'] != NULL) {
    $fullPath = $path.$_GET['download_file'];
    if (file_exists($fullPath)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($fullPath));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($fullPath));
        ob_clean();
        flush();
        readfile($fullPath);
        exit; 
    }
    else {
        echo "<span style=\"font-size: 15px;\">The file you requested to download <span style=\"font-size: 30px; font-weight: bold; color: red; padding: 0px 20px;\">".$_GET['download_file']."</span> does not exists.</span>";
    }
}
else {
    header("location: index.php");
}
?>

更新2:

我使用Mozilla版本21.0测试了代码,它工作正常。但正如我所说,当我点击下载链接时我的计算机冻结了,就在我使用Coolnovo(ChromePlus)版本2.0.8.33的时候。它与浏览器有关吗?

1 个答案:

答案 0 :(得分:0)

尝试清除缓冲区并刷新标头。见下面的例子:

header("Content-type: text/csv");
header("Cache-Control: no-store, no-cache");
header('Content-Disposition: attachment; filename="' . $download_info->original . '"');
ob_clean();   // discard any data in the output buffer (if possible)
flush();      // flush headers (if possible)
readfile($path . $download_info->renamed);

SureVerify - Email Verification