Why does my file being downloaded have my page's HTML appended?
Update:
Never mind. It was happening because it was adding the headers back. So I moved the php code separate from html and it worked. thanks guys
I am using the code below
<?php
$target_path = "Files/";
$target_path = $target_path . basename( $_FILES['decryptfile']['name']);
$fp = fopen('Files/'.$_FILES['file']['name'], 'rb');
header('Content-type: '.$_FILES['file']["type"]);
header('Content-Disposition: attachment; filename="'.$_FILES['file']['name'].'"');
header("Content-Length: " . filesize('decryptedFiles/'.$_FILES['file']['name']));
fpassthru($fp);
exit;
die();
?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Choose a file : <input name="file" type="file" /><br /><p>
Start process</a></center></p>
<p><center><开发者_StackOverflow;input type="submit" value="File" /></center></p>
</form>
My file contents are:
1234567890
when I download the file it shows
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
</head>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Choose a file : <input name="file" type="file" /><br /><p>
Start process</a></center></p>
<p><center><input type="submit" value="File" /></center></p>
</form>
1234567890
Problem is that it appends (includes) the html of my web page to the file... how can I avoid that. thanks
Do a exit
after fpassthru()
.
Also, you should look at the cleaner readfile()
.
If there's any content following the above code, you need to die()
, or it will still be output as normal.
精彩评论