首先...我知道這個問題已經在本網站上討論過很多次,我一直在閱讀過去幾小時的評論和解決方案,但沒有任何幫助。PHP強制下載會產生損壞的文件
我在這裏發佈的代碼已被修剪,但仍包含我面臨的問題。
我創建了一個小腳本來強制使用PHP進行下載。這只是我嘗試在我的網站上使用的代碼的一部分,因爲我不想用太多不相關的代碼向您發送垃圾郵件,但它仍包含錯誤輸出。,因爲它已經得到了解決原來的問題已被刪除:此代碼
一切都與10.6KB
注意的.png文件進行測試。但是,當我將我的代碼片段放入我的網站時,我遇到了另一個問題。
我創建了一個功能,以下載文件:
<?php
function download_file($file)
{
$known_mime_types=array(
"htm" => "text/html",
"exe" => "application/octet-stream",
"zip" => "application/zip",
"doc" => "application/msword",
"jpg" => "image/jpg",
"php" => "text/plain",
"xls" => "application/vnd.ms-excel",
"ppt" => "application/vnd.ms-powerpoint",
"gif" => "image/gif",
"pdf" => "application/pdf",
"txt" => "text/plain",
"html"=> "text/html",
"png" => "image/png",
"jpeg"=> "image/jpg"
);
if(!is_readable($file)) die('<p class="error">File not found or inaccessible!</p>');
$file_extension = strtolower(substr(strrchr($file,"."),1));
if(array_key_exists($file_extension, $known_mime_types)){
$mime_type=$known_mime_types[$file_extension];
} else {
$mime_type="application/force-download";
};
$fsize = filesize($file);
header('Content-Type: ' .$mime_type);
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$fsize);
header('Accept-Ranges: bytes');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Pragma: public');
header('Cache-Control:');
readfile($file);
exit();
}
?>
的的download.php我從中調用該函數:
<!DOCTYPE html>
<?php
require_once 'connect.inc.php';
require_once 'core.inc.php';
require_once 'download_file.php';
?>
<html>
<head>
<title>x3d Download</title>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="css/styles.css" type="text/css"/>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<?php
if (loggedin())
{
include_once 'navbar_loggedin.php';
}
else
{
include_once 'navbar_loggedout.php';
}
?>
<div class="container" width="900px">
<h2>Downloads</h2>
<?php
$sql = "SELECT * FROM `files`";
$result = mysql_query($sql);
if (!$result)
{
echo '<p>No downloads available.</p>';
}
else
{
echo '<table class="table table-hover"><tr>';
echo '<tr><th>Filename</th>';
echo '<th>Filetype</th>';
echo '<th></th>';
if (loggedin())
{
if (getuserlevel($_SESSION['user_id']) == 'Administrator')
{
echo '<th></th>';
}
}
while($row = mysql_fetch_assoc($result))
{
echo '<tr><td><p>'.$row['file_name'].'</p></td>';
echo '<td><p>'.$row['file_type'].'</p></td>';
echo '<td><a href="download.php?download='.$row['file_id'].'"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span></a></td>';
if (loggedin())
{
if (getuserlevel($_SESSION['user_id']) == 'Administrator')
{
echo '<td><a class="red" href="download.php?delete='.$row['file_id'].'"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a></td>';
}
}
}
echo '</tr></table>';
}
?>
<?php
if (isset($_GET['download']))
{
$sql = "SELECT `file_name` FROM `files` WHERE `file_id`='".$_GET['download']."'";
if ($result = mysql_query($sql))
{
$row = mysql_fetch_assoc($result);
$file = "uploads/" . $row['file_name'];
download_file($file);
}
}
if (isset($_GET['delete']))
{
$sql = "SELECT `file_name` FROM `files` WHERE `file_id`='".$_GET['delete']."'";
if ($result = mysql_query($sql))
{
$row = mysql_fetch_assoc($result);
}
if ($row['file_name'] == "")
{
echo '<p class="error">File does not exist.</p>';
}
else
{
$filepath = "uploads/".$row['file_name'];
$sql = "DELETE FROM `files` WHERE `file_id`='".$_GET['delete']."'";
if (file_exists($filepath))
{
try
{
if (unlink($filepath))
{
if ($result = mysql_query($sql))
{
header('Location: download.php');
}
}
}
catch (Exception $e)
{
echo '<p class="error">Could not delete file.</p>';
}
}
}
}
?>
</div>
</body>
</html>
的代碼來調用函數已經過測試,我的SQL查詢確實會返回正確的值。
圖像包含的我的HTML源代碼和原始圖像的一部分...
任何人都可以幫我嗎?
什麼是「失敗的下載會話」?瀏覽器可能輸出的任何特定錯誤?像「由於X無法下載」 –
@Alan Machado瀏覽器錯誤有兩種形式,具體取決於我使用的文件擴展名。 1)C:\ Users \ XX \ AppData \ Local \ Temp \ XX.png.part無法保存,因爲無法讀取源文件。 2)沒有給出這個錯誤,但Firefox只是說下載失敗。 –
我幾乎沒有下載/上傳問題的經驗,但聽起來像服務器不允許恢復已暫停下載時的問題(即使您沒有,但下載管理器[在這種情況下瀏覽器本身]將文件在部分獲取它)。你定義了'Accept-Ranges:bytes;'而不是'Content-Range:bytes = 12345-',這可能是一個原因嗎? (來自[這裏](http://serverfault.com/questions/484091/enable-disable-resuming-downloads-feature-on-the-server-side)) –