Jump to content


Photo

force download with headers...


  • Please log in to reply
3 replies to this topic

#1 hsncool

hsncool
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 09 July 2006 - 04:42 PM

hi.
i dont kno much abt headers, but sum1 gave me a small script to let me make any file download, even html or jpg's, that would normally just open in the browser...

the script goes something lyk this...

<?php

$file=$_GET['file'];
$filename=$_GET['filename'];


if(file_exists("$file")) {
	header("Content-type: application/force-download");
	header("Content-Disposition: attachment; filename=$filename");
	@readfile("$file");
}
exit;

?>

...where "www.haha.com/?file=<X>&filename=<Z>" is the filename of the download...


the problem i am having, is that wen i use the script, the browser nev r knows how large the file is, and is unable to produce an ETA because of this.

i was wondering how it could b edited to enable this, or a new script that can do the same job.

thanx

#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 09 July 2006 - 05:58 PM

When the file is local (on same server as the script) use filesize() as in:

header('Content-Length: ' . filesize($file));

If the $file is a full URL (on another server), then read this topic for another option:

http://www.phpfreaks....html#msg389848

#3 hsncool

hsncool
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 09 July 2006 - 06:02 PM

ooo, it workedd!!! :D
thanx mate!!.
are there any other useful headers that can be used ??, like, for example, changing the text in the title bar of the download box, which the new Windows Live Mail uses, with attatchment downloads. or anything elsee ??

#4 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 09 July 2006 - 06:11 PM

Book recommendation:

HTTP Developer's Handbook (ISBN: 0672324547) by Chris Shiflett.

http://www.amazon.co...=glance&s=books

It does have a little PHP related example code, but it's mostly about the HTTP protocol itself.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users