-1

It's pretty well documented that adding a header is the way to make a link downloadable, but I must be doing something wrong. I write the file, and then produce some HTML linking to it, but the file will not download, only appear in the browser.

<?

   //unique id
   $unique = time();

   $test_name = "HTML_for_test_" . $unique . ".txt";

   //I should put the file name I want the header attached to here (?)
   header("Content-disposition: attachment;filename=$test_name");

   $test_handler = fopen($test_name, 'w') or die("no");

   fwrite($test_handler, $test);
   fclose($test_handler);

?>

<a href="<?=$test_name">Download File</a>
1252748
  • 14,597
  • 32
  • 109
  • 229

2 Answers2

0

Well you are just echoing an HTML tag - you should read the file content instead, like proposed on PHP Doc:

<?php
$file = 'monkey.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');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>
moonwave99
  • 21,957
  • 3
  • 43
  • 64
  • `$file` is just the filename, or should it be the handler. I don't understand how file_exists could work on a text string. – 1252748 Aug 08 '13 at 18:01
  • This doesn't work. It simply prints the file out on the screen. – 1252748 Aug 08 '13 at 18:23
  • @thomas the 'text string' is simply the path to the file that you are checking to see if it exists or not. In this example the file you are asking to check is 'monkey.gif' and it is in the same directory as the program that this code is running in (no path just filename) – Drew Aug 08 '13 at 18:31
0

After much testing, this is the combination that I came up with and it has worked in all my projects ever since.

Program to handle download requests

 <?php

// Do all neccessary security checks etc to make sure the user is allowed to download the file, etc..

// 

 $file = '/path/to/your/storage/directory' . 'the_stored_filename';
 $filesize = filesize($file);
 header('Content-Description: File Transfer');
 header("Content-type: application/forcedownload");
 header("Content-disposition: attachment; filename=\"filename_to_display.example\"");
 header("Content-Transfer-Encoding: Binary");
 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
 header('Pragma: public');
 header("Content-length: ".$filesize);
 ob_clean();
 flush();
 readfile("$file");
 exit;

EDIT

The above code would go into it's own file, for example 'download.php'. Then you would change the download link on your other page to something like:

 <a href="download.php?filename=<?php echo $test_name; ?>">Download File</a>

You will also need to modify the php code above so that it works in your situation. In the example I just gave you would want to change this line:

  $file = '/path/to/your/storage/directory' . 'the_stored_filename';

To this:

  $file = $_get['filename'];

would work at a most basic level. You would want to sanitize the value of $_get['filename'] before just blindly using it in any production environment.

If you want to present the download in the same page that the user is requesting it from then look at my answer to this post: Dowloading multiple PDF files from javascript

Community
  • 1
  • 1
Drew
  • 4,215
  • 3
  • 26
  • 40
  • Thanks drew. This doesn't work for me. It prints the contents out on the screen. If I take out the exit (which I have to do if I want my HTML to render the ``), the link works exactly as before, simply directing the browser to the file. – 1252748 Aug 08 '13 at 18:16
  • Thanks. What do the sharp braces within the href do? Are they in the correct places; they seem a little off. Am I mistaken? – 1252748 Aug 08 '13 at 18:29
  • Why do you need the `$_GET` to get the filename? Can I just say, `$file = "thomas.txt"`? Also, can text files get headers? Maybe I should be setting it to HTML? But then it wouldn't print as code. – 1252748 Aug 08 '13 at 18:31
  • The misplaced alligator in the href was my bad - fixed. The name of the file is passed to the download.php page from whatever page has the href in it. You need to GET the name of the file that was passed in. Not sure what your talking about: text files getting headers ? – Drew Aug 08 '13 at 18:41