Jump to content

File header forcing a download

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
bleastan

bleastan

    Learning Programmer

  • Members
  • PipPipPip
  • 40 posts
Hello been looking for somethig but cant really pinpoint where my problem lies. Ive bee searching the web for a solution first but i cant really find something.

What i basically want is to force a download to a user. I want to do this with headers however instead of forcing the download, what happens is that the file.txt is printed to the screen. Heres my code that i use atm:

<?php
if (file_exists("./files/file.txt")) {
header("Content-type: application/force-download");
header('Content-Disposition: attachment; filename="file.txt"');
readfile('./files/file.txt');
}

?>

As far as im aware this should work but mayb im overlooking something? thx in advance.

edit:
it seems that it works now however, i want to print a form immediately after (got a function called printform() which echoes a form, nothing fancy) but after the download it doesnt seem to execute the function? ayone any ideas?

Edited by bleastan, 20 July 2010 - 02:33 AM.


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Anything printed after the readfile() statement will be appended within the file being downloaded. You can do many things, such as:

<?php
    print "File will be downloaded now."; //you can use printform() here
    header('Location: ./download.php');
?>
Or incase an IF statement within a single file, and point it to itself (through GET parameters).
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
bleastan

bleastan

    Learning Programmer

  • Members
  • PipPipPip
  • 40 posts

Nullw0rm said:

Anything printed after the readfile() statement will be appended within the file being downloaded. You can do many things, such as:

<?php

    print "File will be downloaded now."; //you can use printform() here

    header('Location: ./download.php');

?>
Or incase an IF statement within a single file, and point it to itself (through GET parameters).

Im not quite sure i understand, i can manage to download the file now, however the printForm() still doesnt execute

<?php
if (file_exists("./files/file.txt")) {
header("Content-type: application/force-download");
header('Content-Disposition: attachment; filename="file.txt"');
readfile('./files/file.txt');
header('Location: ./index.php?waarde=printtheform');
//printtheform is a value that is used by a switch to execute function printForm)
}

?>

im not quite sure in which order to place it. Thanks already though.

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
You cannot do anything after you output the header to download, you'd be erroneously adding data to file.txt before it is downloaded. You can only write to the browser before the file is sent.

Run printform() before serving the file, but you can't serve it in the same file unless you separate them. Example:

a.php:
printform();
header('Location: b.php'); //printform will STILL be visible though

b.php:
send file to user (with your code)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
bleastan

bleastan

    Learning Programmer

  • Members
  • PipPipPip
  • 40 posts
but if i try that i get the error:
Warning: Cannot modify header information - headers already sent by etc...
which (as far as im aware) states that somethig is outputted before the header is send

<?php
if (file_exists("./files/file.txt")) {
printForm();
header("Content-type: application/force-download");
header('Content-Disposition: attachment; filename="file.txt"');
readfile('./files/file.txt');

}

?>

Sry if im a bit clueless on this subject but ive never really used headers before. Somethig that i also would like is to delete the file after its downloaded. I know i can use unlink() for that but the thing is, can i execute stuff after the header?

edit:
Got some thigs sorted out. I decided to just make a <A HREF> link to php_self. The header stuff will execute (it downloads the file, and after downloading it deletes it). NOW i want one last thing.... i want to print the form but i still cant do that...
<?php
if (file_exists("./temp/rollback.txt")) {
//printform()
//when i put it here the header wouldnt execute
header("Content-type: application/force-download");
header('Content-Disposition: attachment; filename="file.txt"');


readfile('./temp/file.txt');

unlink('./temp/file.txt');
//printform()
//when i put the function here nothing happened :S
}
?>

This is basically the last thing i need to do , and then ill stop bothering everyone :P. Thanks already.

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
If you wish to display something before a header is sent, you can do two things:
1) ob_start(); : add that to the beginning of the php script, but this is an output buffer hack which really isn't a fix.
2) insead of header('Location: ./foo/file.txt' ); use this: <meta http-equiv=REFRESH CONTENT="5; url=http://your_domain/foo/file.txt">
Then you can use printform() and make them download at the same time (the meta header only forced the download), place the <meta> within the <head></head> tags, of course changing the values to appropriate locations.

But as you're wanting to do delete it, why not make a simple trick?
file.php:
//(the text contents here of file.txt)
<?php
    unlink('./file.php');
?>
And now with that, you can call it like so:
header("Content-type: application/force-download");
header('Content-Disposition: attachment; filename="file.txt"');

readfile('./temp/file.php');
What this does, is it tells PHP to send them a file named "file.txt" composed of file.php's output, therefor they get a text file (file.txt); and then your file.php deletes itself (as in your example).

You may wish to re-study the limitations of headers and rewrite the flow of your code to conform to my examples, there are plenty of ways to get to the end of the path, so I can't really tell you the correct answer.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#7
bleastan

bleastan

    Learning Programmer

  • Members
  • PipPipPip
  • 40 posts
Thanks alot for the help. I managed to fix it yesterday evening. And i figured out (all by myself :D:P) about the unlink file so that works pretty well.

And its true i do need to rework the flow of the code but im pretty much just testing out small functionality things (like this for example, next step is trying to work a bit with xml) so in the end i write the entire program.

anyways thanks for the help.

#8
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Glad you could get it resolved, it seems header() problems are generally ranking in the top 10 problems that newbies face when learning PHP. A lot of people post information that isn't really that correct.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.