0

Im having some major problems with an site im developing, basically whats happening is a user fills out a form, then jquery takes over and posts all the information to sendfile.php, it then is meant to force the user to download a specific file, but its just not doing anything at all and im not seeing any errors either, the file exists.

The code im using is as follows:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename="http://website.com/wp-content/uploads/2012/02/303lowe-logo.jpg"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile("http://website.com/wp-content/uploads/2012/02/logo.jpg");

Any help would be awesome.

yoozer8
  • 7,361
  • 7
  • 58
  • 93
BigJobbies
  • 4,293
  • 12
  • 42
  • 51
  • Are you trying to force people to download a file at an external URL? – Michael Robinson Feb 22 '12 at 03:37
  • I don't recommend forcing users to download files. – yoozer8 Feb 22 '12 at 03:39
  • @MichaelRobinson - No the site and the file are on the same url – BigJobbies Feb 22 '12 at 03:40
  • @Jim - It has to be done this way, client specifically asked for it – BigJobbies Feb 22 '12 at 03:40
  • Why did you include the jquery tag? – shanabus Feb 22 '12 at 03:42
  • the `filename` in the header is the filename you are sending so in your example it should be something like `logo.jpg` the the full URI. Additionally you should use the filesystem path with `readfile` so for if your php file and the wordpress install is in the document root of the site it should be something like `readfile(__DIR__.'/wp-conent/uploads/2012/02/logo.jpg');` – prodigitalson Feb 22 '12 at 03:43
  • Wouldn't there be an issue with trying to force a download with Headers when the user is already in an open browser page? Headers have already been sent and the page is not reloading. – thescientist Feb 22 '12 at 03:45

1 Answers1

3

Based on what you said about using jQuery, I assume you are using AJAX to post the form results to the server. I think you will find that you cannot download a file using AJAX.

Perhaps consider doing the AJAX request then redirecting the user to a new page to download the file. If the redirected page serves the file directly, then the user won't even know they have been redirected (the browser will stay on the same page, usually).

phindmarsh
  • 879
  • 6
  • 11
  • Important exception - if the file type is something that the browser can open (htm/html, jpg, etc) it will just open it, as if it has redirected to a new page. Otherwise, yes, it will just provide a download dialog. – yoozer8 Feb 22 '12 at 03:44
  • @Jim thats right, good catch. Although I think in some cases you can try enforce a download (using attachment headers etc), see: http://stackoverflow.com/questions/5648967/force-download-image – phindmarsh Feb 22 '12 at 03:47
  • Yes, I've read a bit about that on SO (I needed to do exactly that). However, it requires obfuscating the MIME type so the browser won't try to open it, which means the user then has to figure out which program to use to open it on their own rather than it automatically opening in the correct application based on MIME type. – yoozer8 Feb 22 '12 at 03:49
  • Yeah, its better not to try and be tricky in the first place :) – phindmarsh Feb 22 '12 at 03:49
  • I actually [asked a question](http://stackoverflow.com/q/7662363/866022) about just that and got a useful answer. – yoozer8 Feb 22 '12 at 03:49
  • @Jim: no, when you send out a content disposition of 'attachment', the browser will download it/present a save-as dialog. if it's "inline", it'll try to display it if it knows how, or again show a save-as dialog for unknown types. – Marc B Feb 22 '12 at 03:50
  • @MarcB From [this article on the topic](http://www.jtricks.com/bits/content_disposition.html), "Content-Type header should refer to an unknown MIME type (at least until the older browsers go away)." – yoozer8 Feb 22 '12 at 03:52