Jump to content

[SOLVED] Download a php file


payney

Recommended Posts

This is such a technical php question, but does anyone know how to do the following.

 

I simply want to post a link on my site, which will allow the user to download a php file. Without it open in the page, so when the user clicks on it, it says open/save etc.

 

Is this possible? This just opens the page:

 

  <a href="un_template.php">Download Template </a>

Link to comment
https://forums.phpfreaks.com/topic/75368-solved-download-a-php-file/
Share on other sites

<?php
$filesize = filesize($file);

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 

header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($file));

header("Content-Description: File Transfer");
header('Accept-Ranges: bytes');
header('Content-Length: ' . $filesize);
readfile($file);
?>

Where $file is the url to the wanted .php file.

 

 

EDIT: note, if you set the filesize like in my example, your browser can correctly estimate download times.

Also you should use application/force-download, to ensurw that it is downloaded.

Use GET to set the $file.

I  think using get to call the file is a bad idea unless you filter the input,

Obviously you'd filter it. But it makes it easier, if you've got lots of files just to send a request to one file which reads the GET value, instead of having to use several different files for each download. Most sites use GET for downloads, filter the input to make sure it's valid, not harmful etc.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.