简体   繁体   中英

PHP Download File with Include

How can I use PHP's include function to include a file and then modify the headers so it forces - at least that's how it's called - browsers to download ITSELF (the PHP file). Is it possible to also modify the preset save name, in order to change the extension from *.php to something else?

Thanks in advance!

PHP include function will parse the file. What you want to do is use file_get_contents or readfile .

Here's an example from the readfile documentation:

$file = 'somefile.gif';

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;
}

Change the headers to suit your particular needs. Check out the above links for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM