好的,我把文件上传到我的数据库好了。但是从数据库下载文件时出现问题,它显示为链接,当我点击链接时没有任何反应。
我仍然是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;
}
?>
答案 0 :(得分: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;
}
?>
答案 1 :(得分:0)
在download.php
中,您可以创建链接exit
。这意味着您不会再进行exit
来电了。
<?php
}
}
exit;
mysql_close()
?>
您需要在下载文件中重新考虑您的逻辑。你也打电话给
header("Content-length: $size");
exit;
print $content;