0

I've tried many PHP scripts to build a force download page but failed every time. I have a download link on each page and clicking which I want user to be redirected to another page and the download begins.

I'm passing the full absolute path URL in the parameter as <a href="download.php?filename=http://www.abc.com/maps/map.pdf" target="_blank">Download PDF</a>

the code of my download.php file is

<?php
$file = $_GET["filename"];
    $file_name = basename($file);
    header("Content-Type: application/zip");
    header("Content-Disposition: attachment; filename=$file_name");
    header("Content-Length: " . filesize($file)); //added this line
    readfile($file);
?>

If your download doesn't start

<a href="<?php echo $_GET["filename"]; ?>" target="_blank">click here</a>

Errors:

Download.php is getting the parameter filename value correctly but says 

Warning: Cannot modify header information - headers already sent by (output started at /home/content/72/11973872/html/filerepairtool/down/download.php:9) in /home/content/72/11973872/html/filerepairtool/down/download.php on line 20

Warning: Cannot modify header information - headers already sent by (output started at /home/content/72/11973872/html/filerepairtool/down/download.php:9) in /home/content/72/11973872/html/filerepairtool/down/download.php on line 21

I want to build a page like the download page in most of the software download sites where clicking the download link takes us to another URL which says something like "Your download should begin in 10 seconds (With second countdown) OR Click here to download if it doesn't start"

Please share the solution

Albzi
  • 15,431
  • 6
  • 46
  • 63
Rajesh Kumar
  • 17
  • 1
  • 6
  • Try adding `ob_start();` above `$file = $_GET["filename"];` and then adding `ob_end_flush();` below `readfile($file);` --- You may also have a space before ` – Funk Forty Niner Mar 28 '14 at 16:05
  • You should check the file that is being requested, otherwise one could download any file in the current directory. – Gumbo Mar 28 '14 at 16:11

1 Answers1

0

Your code checks out, I tested it.

Something else is causing this.

You most likely have a space before <?php which reproduced the exact error message when testing.

You could also have a byte order mark or other form of output, used as an included file, etc.

  • Even a space counts as outputting before header.

Possible scenarios:

  • You have more PHP and/or HTML above your PHP
  • A cookie
  • A byte order mark
  • An included file above your PHP
  • etc.

Sidenote:
From what I saw in another question you posted, you may be including a file above your PHP.


The following will cause an error: (consided as output before header)

<?php
include 'file.php';
?>

<?php
$file = $_GET["filename"];
    $file_name = basename($file);
    header("Content-Type: application/zip");
    header("Content-Disposition: attachment; filename=$file_name");
    header("Content-Length: " . filesize($file)); //added this line
    readfile($file);
?>

as will:

<!DOCTYPE html>
<head>
<title>Page title</title>
<body>
Hello world

<?php
$file = $_GET["filename"];
    $file_name = basename($file);
    header("Content-Type: application/zip");
    header("Content-Disposition: attachment; filename=$file_name");
    header("Content-Length: " . filesize($file)); //added this line
    readfile($file);
?>
</body>
</html>

Possible quick fix: but not always successful.

Using ob_start(); and ob_end_flush();

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141