I am developing a site that will allow the users to download files. The files could be of various types - documents, images, applications, etc. I am using a simple link to offer the download

<A HREF="File_To_Download"> Download </A>

If the browser does not support the type of file being downloaded, it simply offers the user an option to the download the file, exactly what I want.
The problem is if the browser can handle the type of file being download (say image files), it opens the file. I would prefer that it offers the user the option to download all different types of files. How do I achieve this ?

I am not sure if this can be done in HTML, or if I need javascript for that. Hence I am posting in this forum. If javascript is required, that is absolutely fine for me.

Thanks

Dani AI

Generated

Short answer: make the server tell the browser to save the file. A plain link will be opened by browsers that can render the file type, so client-side tricks are unreliable. Since is on PHP/Linux, send a download response from PHP. was right that a regular href depends on browser support, but headers are the robust fix.

Example PHP pattern (validate paths and authenticate before this):

<?php
$file = '/path/to/private/files/example.pdf';

if (!is_readable($file)) {
  http_response_code(404);
  exit;
}

$basename = basename($file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $basename . '"; filename*=UTF-8\'\'' . rawurlencode($basename));
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('X-Content-Type-Options: nosniff');

flush();
readfile($file);
exit;
?>

Notes and troubleshooting:

  • Use the true MIME type where known (instead of application/octet-stream) if you want; Content-Disposition: attachment is what forces "save as". See MDN: Content-Disposition header.
  • Always validate the requested file (map an ID to a path, avoid user-supplied file paths, check permissions). Never expose arbitrary filesystem paths.
  • For non-ASCII filenames include a filename*= UTF-8 percent-encoded fallback as shown above for broad compatibility.
  • For very large files avoid PHP streaming (memory/timeout issues). Use server features like X-Sendfile / X-Accel-Redirect so the webserver handles the file transfer after PHP authorizes it.
  • If you need a client-only hint for same-origin links, the HTML5 download attribute can help, but it is limited and not a substitute for server headers.

Refer to the PHP manual for header() and readfile() usage for more details: PHP: header and PHP: readfile.

Recommended Answers

All 2 Replies

just the regular href will choose to view in the browser based upon what the browser can do

here's a link for javascript to force download

Thanks dickersonka for the link. I checked it out, but it doesn't really help me. For one, the execCommand("SaveAs" ..) supposedly works only for IE, so that solution is unacceptable. Changing the filename to remove the extension part - to me sounds like a bad way of doing things, especially since I am not limited to pdf files only. As for C# and .NET , I am using PHP-Linux, so that doesn't help me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.