简体   繁体   English

使用mysql和PHP从服务器下载文件

[英]File download from server using mysql and PHP

I have created a PHP page that allows users to download a file when they click the this link: 我创建了一个PHP页面,当用户单击此链接时,该页面允许用户下载文件:

<a href="download_pdf.php?pubid=<?php echo $row_rsPublications['pubID']; ?>&amp;file=<?php echo $row_rsPublications['file']; ?>">Download File</a>

I have also created the download page that the link directs to: 我还创建了链接指向的下载页面:

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

}
?>

Now my problem is, when the download popup window come up, it reads that the file is of 0 bytes. 现在我的问题是,当出现下载弹出窗口时,它读取文件为0字节。 And when downloaded, it can't open. 而且下载时无法打开。 i get the message that it is not a supported file type or its been damaged or corrupted. 我收到消息,提示它不是受支持的文件类型,或者它已损坏或损坏。

Please any help would be much appreciated. 请任何帮助将不胜感激。

You're not doing anything with the query result in $row. 查询结果行没有执行任何操作。

Use $row['file'] to get the actual file itself. 使用$ row ['file']来获取实际的文件本身。

Thank you all for assisting me. 谢谢大家的帮助。 I have been able to solve the problem after further reading. 进一步阅读后,我已经能够解决问题。 I have updated the initial script i wrote. 我已经更新了我编写的初始脚本。 what is above is now the working script. 以上是工作脚本。 What i did was to include $fullpath = $path . $filename 我所做的是包括$fullpath = $path . $filename $fullpath = $path . $filename then changed the header("Content-Disposition: attachment; filename=\\"$filename\\""); 然后$fullpath = $path . $filename更改header("Content-Disposition: attachment; filename=\\"$filename\\""); and then the readfile function from readfile($path) to readfile($fullpath) . 然后将readfile函数从readfile($path)更改为readfile($fullpath) Thanks again @nlsbshtr and everybody else for your help. 再次感谢@nlsbshtr和其他所有人的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM