2015-10-14 56 views
3

首先...我知道这个问题已经在本网站上讨论过很多次,我一直在阅读过去几小时的评论和解决方案,但没有任何帮助。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源代码和原始图像的一部分...

任何人都可以帮我吗?

+0

什么是“失败的下载会话”?浏览器可能输出的任何特定错误?像“由于X无法下载” –

+0

@Alan Machado浏览器错误有两种形式,具体取决于我使用的文件扩展名。 1)C:\ Users \ XX \ AppData \ Local \ Temp \ XX.png.part无法保存,因为无法读取源文件。 2)没有给出这个错误,但Firefox只是说下载失败。 –

+0

我几乎没有下载/上传问题的经验,但听起来像服务器不允许恢复已暂停下载时的问题(即使您没有,但下载管理器[在这种情况下浏览器本身]将文件在部分获取它)。你定义了'Accept-Ranges:bytes;'而不是'Content-Range:bytes = 12345-',这可能是一个原因吗? (来自[这里](http://serverfault.com/questions/484091/enable-disable-resuming-downloads-feature-on-the-server-side)) –

回答

4

你的代码很好。但是,你正在下载是一个致命的错误,而不是图像:

<br /> 
<b>Fatal error</b>: Call to undefined function fileread() in <b>/var/www/html/test.php</b> on line <b>18</b><br /> 

变化fileread($file);readfile($file);,它应该工作。

下次您有“140字节的损坏文件”时,尝试将其作为文本文件打开。

+0

良好的通话,我错过了它'逆转':) –

+0

这是一个巨大的facepalm>。<非常感谢!它确实解决了我的代码中的问题,现在我将尝试在我的其他代码中实现它,但我相信它会起作用。 –

+0

当我尝试通过使用函数调用此代码段时,我仍然有损坏的文件... –