gpt4 book ai didi

php - 从MYSQL数据库下载文件

转载 作者:行者123 更新时间:2023-11-29 13:57:50 25 4
gpt4 key购买 nike

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

我对 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;
}
?>

最佳答案

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

<?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;
}
?>

关于php - 从MYSQL数据库下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15560441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com