我想創建一個下載鏈接,我在網上找到了一個例子。我忘了我在哪裏得到它。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時的情況。它與瀏覽器有關嗎?
此代碼將繼續讀文件,直到它到達其終點。如果您嘗試使用大文件,它可能會消耗大量系統資源以迴應文件內容,直到您下載整個文件 –
Hi @FilipposKarapetis,我該如何解決這個問題?我能舉一個例子嗎?發佈的代碼是從Internet例子中複製的,而我在'文件處理「方面不擅長。 – Chin
Filippos有一個很好的觀點,您試圖下載的文件的大小是多少? – phpisuber01