user5646514
user5646514

Reputation: 69

How to make a link to download a server file on the browser?

I am trying to create a simple download link

The file I want to download is a .pdf that the user can upload, and now I want to make it possible to download. With the pop-up box, you know, or just on the chrome download bar

I've tried what every answer says:

<a href="<?=$GUIDELINES_PDF_DIR . $projectId . '.pdf'?>" download="guidelines.pdf">Download PDF</a>

But that leads to "Failed - no file", even though when I check

 if(file_exists($GUIDELINES_PDF_DIR . $projectId . '.pdf'))

it returns true!

Some other notes:

Thank so much :)

Upvotes: 1

Views: 818

Answers (2)

Vladmir
Vladmir

Reputation: 575

Use a right headers:

header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='downloaded.pdf'");

Upvotes: 0

Quentin
Quentin

Reputation: 943108

$GUIDELINES_PDF_DIR is a directory on your server's hard disk.

The resulting path you are creating is relative to the root of your server's hard disk and not to the DocumentRoot of your web server.

You need to account for that difference when generating your URL.

Upvotes: 1

Related Questions