0

好的,我将文件上传到我的数据库中。但是从数据库下载文件时出现问题,它显示为一个链接,当我单击该链接时没有任何反应。

我对 php 还是很陌生,所以我的代码并不完美。

这是我的上传文件

 <?php

   require 'connect.php';
 ?>
 <form method="post" enctype="multipart/form-data">
 <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
 <tr>
 <td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value="                                        
</tr>
  </table>
  </form>


 <?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileType = $_FILES['userfile']['type'];
$fileSize = $_FILES['userfile']['size'];
$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, type, size, content) ".
"VALUES ('$fileName', '$fileType', '$fileSize', '$content')";
mysql_query($query) or die('Error, query failed');
echo "<br>File $fileName uploaded<br>";
}
?>



 Here is my download 


<?php
require 'connect.php';

   $query = "SELECT id, name FROM upload";
   $result = mysql_query($query) or die('Error, query failed');
   if(mysql_num_rows($result) == 0)
   {
    echo "Database is empty <br>";
   }
   else
   {
    while(list($id, $name) = mysql_fetch_array($result))
    {
   ?>
   <a href="download.php?id=<?php echo urlencode($id);?>"><?php echo urlencode($name);? ></a> <br>
   <?php
   }
   }
   exit;
  mysql_close()
   ?>
   <?php
   require 'connect.php';
    $query = "SELECT id, name FROM upload";
    if(isset($_GET['id']))
    {

    $id    = $_GET['id'];
    $query = "SELECT name, type, size, content " .
    "FROM upload WHERE id = '$id'";
     $result = mysql_query($query) or die('Error, query failed');
    list($name, $type, $size, $content) =  mysql_fetch_array($result);
    $content = $row['content'];
    header("Content-Disposition: attachment; filename=\"$filename\"");
    header("Content-type: $type");
    header("Content-length: $size");
    exit;
    print $content;
   ob_clean();
  flush();
    echo $content;
   }
    ?>
4

2 回答 2

1

我彻底扫描了您的代码,这是最终答案。

<?php
    require 'connect.php';

    $query = "SELECT id, name FROM upload";
    $result = mysql_query($query) or die('Error, query failed');

    if(mysql_num_rows($result)==0){
        echo "Database is empty <br>";
    }
    else{
        while(list($id, $name) = mysql_fetch_array($result)){
            echo "<a href=\"download.php?id=\$id\">$name</a><br>";
        }
    }

    if(isset($_GET['id'])){
        $id    = $_GET['id'];   
        $query = "SELECT name, type, size, content FROM upload WHERE id = '$id'";       
        $result = mysql_query($query) or die('Error, query failed');
        list($name, $type, $size, $content) =  mysql_fetch_row($result);
        header("Content-Disposition: attachment; filename=\"$name\"");
        header("Content-type: $type");
        header("Content-length: $size");
        print $content;
    }
?>
于 2013-03-22T00:12:02.877 回答
0

在你的download.php你做链接和exit。这意味着您不会比您的exit电话更进一步。

<?php
   }
   }
   exit;
   mysql_close()
 ?>

您需要重新考虑下载文件中的逻辑。你也叫

header("Content-length: $size");
exit;
print $content;
于 2013-03-22T00:12:43.797 回答