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

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);

 

Link to comment
Share on other sites

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');
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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