user3053216
user3053216

Reputation: 809

php force download file not working

I am trying to make make a visitor download text file when visiting a certain page. This is the page:

<?php
$file_url   = 'text.txt';
header("Content-Disposition: attachment; filename=\"" . basename($file_url) . "\"");
header("Content-Type: application/force-download");
header("Content-Length: " . filesize($file_url));
header("Connection: close");
readfile($file_url)
?>

However, the file just shows up in the browser, in stead of asking for downloading. What am i doing wrong?

Upvotes: 0

Views: 1424

Answers (1)

Baraa
Baraa

Reputation: 1361

You might be missing some header properties.

<?php
    $file_url = 'text.txt';

    header('Content-Type: text/plain');
    header('Content-Disposition: attachment; filename='.basename($file_url));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file_url));
    readfile($file_url);
    exit;
?>

Upvotes: 1

Related Questions