mysql document upload linking

I have an upload form

documents are uploaded to an (Uploads/Reference/) folder and a link to document is stored in mysql database like this:

$sql="INSERT INTO uploaddate (upload_id, file_link)
            VALUES
            ('$upload_id', '$target')";

the link stored in 'file_link' column of 'uploaddata' table is like this:

(Uploads/Reference/6206webhost change.txt)

Now when i run a select statement to this table, instead of just displaying plain text extension i would like to get a clickable link which can be used to download the uploaded document from it's location.

How can i do this?

Upvotes: 0

Views: 163

Answers (1)

Marco
Marco

Reputation: 57573

You could do this: when you render page using PHP, create a link <a href="/download.php?id=doc_id">Download</a>.
Then in a new page called download.php get document id, read filename from db using that doc_id and send that file out so user can download it...

EDITED:

  • with $_GET['id'] you get doc_id: be careful, check if value really exists and sanitize it
  • Query your db; e.g. SELECT file_link FROM uploaddate WHERE upload_id = doc_id;
  • Take a look to the accepted answer here

I write the example from other post, but check that to get alla informations:

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}

Upvotes: 1

Related Questions