简体   繁体   中英

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:

<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. 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.

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 then changed the header("Content-Disposition: attachment; filename=\\"$filename\\""); and then the readfile function from readfile($path) to readfile($fullpath) . Thanks again @nlsbshtr and everybody else for your help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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