user3732711 user3732711 - 1 year ago 109
MySQL Question

Download file from server folder

I have files uploaded on server folder and its path is saved in database. sample view of table is:

id name resume
1 N1 resume/abc.doc
2 N2 resume/def.pdf

On a click of a button i wish to download the files (format of file would mostly be docx, pdf) from the server to the system. The code that i have written downloads a file but it is either empty or corrupted. Can anyone please tell how to download files from server

<a href="download.php?candidateid=<? echo $candidateid ?>">Download</a>



$filename = "Filename.docx";
header("Content-Disposition: attachment; filename=\"$filename\"");
$flag = false;
$sql = "SELECT resume FROM candidates where id='".$candidateid."' ";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0)
while($row = mysqli_fetch_assoc($result))
if (!$flag)
// display field/column names as first row
echo implode("\t", array_keys($row)) . "\r\n";
$flag = true;
echo implode("\t", array_values($row)) . "\r\n";

Sam Sam
Answer Source

Using the candidateid you can fetch the file name from your table and then store in a variable and then write the code

$file = '/path/to/your/dir/'.$file;

if(!$file){ // file does not exist
    die('file not found');
} else {
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=$file");
    header("Content-Type: application/zip");
    header("Content-Transfer-Encoding: binary");

    // read the file from disk
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download