0

I'm new to PHP and I'm trying to create a form which when the user fills out, is redirected to my success page and then a download is initiated and their details are saved to a .txt file.

I've managed to do all of the above but the user isn't redirected, but the code to download the file still runs (which is located in the success page).

Why is this happening?

If I removed the code to download the file then the user is redirected, do I need to add a delay before the file download?

Index.php

<!DOCTYPE HTML>
<html>
<head>

</head>
<body>
<form action="myform.php" method="post">

Name <input type="text" name="name" value="">
Company <input type="text" name="company" value="">
Website <input type="text" name="website" value="">
Email <input type="text" name="email" value="">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

myform.php

<?php
$name = @$_POST['name']; 
$company = @$_POST['company']; 
$website = @$_POST['website']; 
$email = @$_POST['email']; 

$filename = "info.txt";

 $f_data= '
Name : '.$name.'
Email :  '.$email.'
Website: '.$website.'  
Company: '.$company.'  
=============================================================================    =
';
 $file = fopen($filename, "a");
fwrite($file,$f_data);
fclose($file);

header("Location:success.php");

?>

success.php

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
header("Content-Disposition: attachment; filename=guide.pdf");

header("Content-Type: application/octet-stream");

header("Content-Length: ".filesize("guide.pdf"));

header("Pragma: no-cache");

header("Expires: 0");

$fp = fopen("guide.pdf", "r");

print fread($fp, filesize("guide.pdf"));

fclose($fp);

exit();

?>
<html>
<h1>Congrats</h1>
</html>    
Joe D
  • 49
  • 9
  • you're outputting before header – Funk Forty Niner Sep 27 '15 at 15:02
  • Headers must be output before any content - you'd know this if you had errors enabled in your php installation (and had E_NOTICE visible) – h2ooooooo Sep 27 '15 at 15:03
  • Okay so how do I resolve this? Moving the echo to after the download code still yields the same result – Joe D Sep 27 '15 at 15:10
  • no, "echo" is output (remove it like a bad tooth). Use error reporting to troubleshoot http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Sep 27 '15 at 15:12
  • Okay, I've removed the echo and replaced it when a

    Success!

    at the end of the code instead, still the same result :/
    – Joe D Sep 27 '15 at 15:23
  • 1
    Update your question with what you're now using. My guess is, you're still outputting before header. Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Sep 27 '15 at 15:24
  • Same for `print`, that's output. Plus, `@` are error suppressors; remove them for testing. – Funk Forty Niner Sep 27 '15 at 15:30
  • You can't output after `exit;` either. And again, there is **no way** to mix a file download and HTML output. You can't mash them in either order. -- The typical workaround is printing the status page before download, not using a `Location:` header, but `` to trigger the download a bit later. – mario Sep 27 '15 at 15:33
  • Your suggestion worked a treat @mario , that was exactly what I was after – Joe D Sep 27 '15 at 16:52

1 Answers1

0

success.php is a download. Your browser will not redirect to the download, it will just download it. You will need to move your download code to another file - say, download.php, and redirect to that. It may be better user experience to have a "download your report" button rather than an automatic download.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592