从MySQL数据库下载文件

时间:2013-03-17 18:38:47

标签: php mysql database download

我似乎无法在线下载我上传到MySQL数据库中的文件我只能获得纯文本,模糊的标志。在localhost虽然下载没问题。 我会告诉你Upload.php(它不是真的称之为,而且这也不是整个剧本,它很大)首先:

if (isset($_POST['verstuur'])) {
$id = NULL;
if($_FILES['userfile']['size'] > 0)
    {
    $fileName = $_FILES['userfile']['name'];
    $tmpName  = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];

    $fp      = fopen($tmpName, 'r');
    $content = fread($fp, filesize($tmpName));
    $content = addslashes($content);
    fclose($fp);

    if(!get_magic_quotes_gpc())
    {
        $fileName = addslashes($fileName);
    }
    $query = "INSERT INTO upload (name, size, type, content ) ".
    "VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
    mysql_query($query) or die('Fout, query mislukt'); 
    $query = "SELECT LAST_INSERT_ID() ID";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result)){
    $id = $row['ID'];
    }
} 

好的,我有另一个脚本,我在其中创建下载链接:

$query = "SELECT U_ID, name FROM upload WHERE U_ID = '$opvraag[U_ID]'";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
     echo "<td>Geen Bijlage</td>";
} 
else
{
     while(list($id, $name) = mysql_fetch_array($result))
     {
         echo '<td><a href="download.php?id='.$opvraag['U_ID'].'">'.$name.'</a></td>';
     }
}

最后我有download.php

if(isset($_GET['id'])) 
{
   $id    = $_GET['id'];
   $query = "SELECT name, type, size, content " .
         "FROM upload WHERE U_ID = '$id'";
   $result = mysql_query($query) or die('Fout, query mislukt');
   list($name, $type, $size, $content) =  mysql_fetch_array($result);
   header("Content-length: $size");
   header("Content-type: $type");
   header("Content-Disposition: attachment; filename=$name");
   echo stripslashes($content);
   exit;
}

现在苦苦挣扎了2天,谷歌也没有帮助我。 在此先感谢。

0 个答案:

没有答案