我已经创建了一个PHP页面,允许用户在单击此链接时下载文件:
<a href="download_pdf.php?pubid=<?php echo $row_rsPublications['pubID']; ?>&file=<?php echo $row_rsPublications['file']; ?>">Download File</a>我还创建了链接指向的下载页面:
<?php
if(isset($_GET['file'])) {
$fileID = $_GET['pubid'];
$filename= ($_GET['file']);
$path = "admin/pubfiles/";
$fullPath = $path . $filename;
mysql_select_db($database_connDioceseofife, $connDioceseofife);
$sql = "SELECT file FROM publications WHERE pubID = $fileID";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if($filename == NULL) {
die('No file exists or the name is invalid!');
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=\"$filename\"");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
readfile($fullPath);
}
?>现在我的问题是,当下载弹出窗口出现时,它显示该文件为0字节。下载后,它就打不开了。我得到的消息是它不是受支持的文件类型,或者它已损坏或损坏。
请提供任何帮助,我们将不胜感激。
发布于 2013-01-26 22:39:18
您没有对$row中的查询结果做任何操作。
使用$row' file‘获取实际的文件本身。
发布于 2013-01-27 01:58:28
感谢大家对我的帮助。经过进一步的阅读,我已经能够解决这个问题了。我已经更新了我编写的初始脚本。上面的就是现在的工作脚本。我所做的是包含$fullpath = $path . $filename,然后将header("Content-Disposition: attachment; filename=\"$filename\"");和readfile函数从readfile($path)更改为readfile($fullpath)。再次感谢@nlsbshtr和其他所有人的帮助。
https://stackoverflow.com/questions/14537894
复制相似问题