2013-06-19 38 views
0

我想创建一个下载链接,我在网上找到了一个例子。我忘了我在哪里得到它。PHP下载文件冻结了电脑

下面是代码:

<?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服务器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时的情况。它与浏览器有关吗?

+0

此代码将继续读文件,直到它到达其终点。如果您尝试使用大文件,它可能会消耗大量系统资源以回应文件内容,直到您下载整个文件 –

+0

Hi @FilipposKarapetis,我该如何解决这个问题?我能举一个例子吗?发布的代码是从Internet例子中复制的,而我在'文件处理“方面不擅长。 – Chin

+0

Filippos有一个很好的观点,您试图下载的文件的大小是多少? – phpisuber01

回答

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

+0

我认为我的代码的主要问题是必须下载的文件,如果不是系统会继续读取文件。 'readfile()'和'fread'有相同的问题吗? – Chin

+0

我已更新我的代码,但我仍然遇到同样的问题。 – Chin

+0

它与浏览器有关吗?因为这两个代码在Mozilla中都能正常工作,但不是Coolnovo(ChromePlus)。 – Chin