Force a file to be downloaded

To force a file to be downloaded from a web page, you can use the Content-Disposition header with the value "attachment" in the HTTP response. This header tells the browser to treat the file as an attachment and initiate a download instead of displaying it in the browser window.

Here's an example of how you can set the Content-Disposition header in different programming languages:

$file = '/path/to/file.ext';
header('Content-Disposition: attachment; filename="' . basename($file) . '"');

**Python (with Flask):**
from flask import Flask, send_file

app = Flask(__name__)

def download_file():
file_path = '/path/to/file.ext'
return send_file(file_path, as_attachment=True)

if __name__ == '__main__':

**Node.js (with Express):**
const express = require('express');
const app = express();

app.get('/download', (req, res) => {
const file = '/path/to/file.ext';;

app.listen(3000, () => {
console.log('Server is running on port 3000');

In each of these examples, the `/path/to/file.ext` should be replaced with the actual path of the file you want to force the download for.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Changing PHP settings in an .htaccess file

To change PHP settings using an `.htaccess` file, you can use the `php_value` directive. This...

Control file extensions with an .htaccess file

To control file extensions using an `.htaccess` file, you can use Apache's mod_rewrite module to...

How can I redirect and rewrite my URLs?

To redirect and rewrite URLs, you can use a combination of server configuration and URL rewriting...

Deny access to a site with an .htaccess file

To deny access to a site using an .htaccess file, you can use the `Deny` directive in combination...

Force your site to load securely code for .htaccess

To force your website to load securely using the .htaccess file, you can add the following code...