php - 如何在php中使文件下载仅将文件路径保存在数据库中

标签 php mysql file download

我是开发领域的新手,所以提前对愚蠢的问题表示抱歉。 我正在创建一个上传 pdf 文件并存储在目录 admin/uploads 中的脚本,我将文件路径存储在数据库中,我的完整代码如下:

$image="uploads/".$_FILES['image']['name'];
$pdf="uploads/".$_FILES['pdf']['name'];
mysql_query("insert into novels                 values('','$title','$author','$image','$pdf','$body','$Keywords',$cat)");
header("location:add-novel.php?cat=$cat");
?>

我的表格是:

<form method="post" action="store-novel.php?cat=<?php echo $_REQUEST['cat'];?>"         enctype="multipart/form-data">
<table width="500" border="0"  align="left" class="innerTable">
<tr>
<td colspan="2" class="th">ADD NEW NOVEL</td>
</tr>

<tr>
<td>Title</td>
<td><input type="text" name="title" id="title" /></td>
</tr>
<tr>
<td>Author</td>
<td><input type="text" name="author" id="author" />
<!--<input type="hidden" name="category" value="<?php echo $_REQUEST['cat'];?>" />-->
</td>
</tr>
<tr>
<td>Image</td>
<td class="button"><input type="file" name="image" id="image" /></td>
</tr>
<tr>
<td>PDF</td>
<td><input type="file" name="pdf" id="pdf" /></td>
</tr>
<tr><td>post body</td>
<td><textarea name="body" id="body" cols="30" rows="15"></textarea></td>
</tr>
<tr>
  <td>Keywords</td>
  <td><input type="text" name="Keywords" id="Keywords" /></td>
</tr>
<tr><td></td><td><input type="submit" name="button" id="button" value="Submit" /></td>
</tr>
</table>
</form>

我想要一个下载页面,用户可以从中下载我的 pdf 文件

最佳答案

您想下载服务器上存储的文件吗?

试试这个:

$size = filesize($yourfile);

header("Content-Type: application/octet-stream");

header("Content-Disposition: attachment; filename='$filename'");

header("Content-Length: $size");

readfile($yourfile);

关于php - 如何在php中使文件下载仅将文件路径保存在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20354205/

相关文章:

PHP 特征 : is there a proper way to ensure that class using a trait extends a super class which contains certain method?

php - SQL 语句不写入数据库

mysql - 从存储过程填充下拉列表错误: was out of range of valid values

php - mySQL UPDATE table based on SELECT (count) of a different table

php - Symfony 4 生产模式错误处理页面内存耗尽

php - 通过一个mysql查询插入到不同的表中

php - 我应该使用哪个索引来混合 WHERE 和 ORDER BY

.net - 如何在服务器上创建 Excel 文件并将其返回给用户后自动删除?

c - 使用文件写入记录并在c中查看

file - vbs所有用户桌面文件夹