Jump to content

[SOLVED] PHP to download file througe header


chathura86

Recommended Posts

I want to make the download through the header commands.

 

it is a txt file and i used

 

header("Content-Disposition: attachment; filename=\"" . $myFile . "\"");

readfile($myFile);

 

but since i have other html codes below this line when i saved the file it also contains the html source also

 

i want to show the html page while doing the downloading through header commands

 

i have seen this in many sites

 

like browser popups for the download but page displays with

 

'click here if your download does not start in few seconds'

 

 

hope my question is clear

 

Regards

<?php 
// We'll be outputting a PDF
header('Content-type: application/txt');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="somefile.txt"');

// The PDF source is in original.pdf
readfile('somefile.txt');
?>

dont forget to replace the somefile with ur own txt file name,

Ted

this is the page u want html to store in:

<?php 

echo "test";?>
<script type="text/javascript">
<!--
window.location = "test.php"
//-->
</script>

download page separate here:

<?php 
header('Content-type: application/txt');
header('Content-Disposition: attachment; filename="3.txt"');
readfile('3.txt');
?>

Ted

On html page:

 

<a href="download.php?file=file.txt>Download</a>

 

download.php:

include(fileforce.php); // this is the file in the link I posted earlier

set_time_limit(0);	
$file_path=$_GET['file'];
$name = $_GET['file'];
output_file($file_path, $name);

 

HTML PAGE

Your download will begin shortly. If it does not start, click <a href="download.php">download</a>.
<script type="text/javascript">
window.onload = function(){
  window.location = 'download.php';
}
</script>

 

DOWNLOAD PAGE

<?php
header('Content-type: application/txt');
header('Content-Disposition: attachment; filename="file.txt"');
readfile('file.txt');
?>

 

Archived

This topic is now archived and is closed to further replies.

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