Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.