IE problem w/ header() for downloads

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

IE problem w/ header() for downloads

Post by Galahad »

I wrote a script to hide actual file locations from the user. In order to download a file, the user has to go to download.php?file=123 (whatever the file number is). That page checks to make sure they are logged in, have access to the file, etc.

It works great, for the most part. When I try it on my local machine (see below for server info), it works great. When I try it on the actual server, it will not work correctly in ie for windows. All the other browsers are fine, though.

Here's some sample code:

Code: Select all

<?php
  // Make sure file exists and open it up here

  header("Cache-Control: public\n");
  header("Pragma: no-cache\n");
  header("Content-type: $type\n");
  header("Content-disposition: attachment; filename="$obj->Filename"\n");
  header("Content-transfer-encoding: $encoding\n");
  header("Content-Length: $size\n");
  header("Connection: close\n");

  // Read the actual file, 4k at a time
  while (!feof($fp)) {
    $buffer = fread($fp, 4096);
    print $buffer;
  }
  fclose($fp);
?>
Here's the server situation: we have a server running php 4.2.2 with apache 2.0.40 on redhat 8. I have a local test machine running the same stuff, but I have a slightly more up-to-date system (I run up2date more frequently). There is a chance our php/apache versions are not identical, but nothing major. I've gone through a diff of the httpd.conf files, and the php.ini files but can't find anything that seems like it should cause it. I've read the comments on the header() man page, but none of those seemed to help. What really confuses me is that it works with ie off my machine, but not the server. Any ideas?
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

This should work on all browsers.

Code: Select all

<?php
chdir($_REQUEST['dir']);
$name=$_REQUEST['filename'];

header("Content-disposition: attachment; filename=$name");
header("Content-type: application/download");
header("Pragma: no-cache");
header("Expires: 0");
readfile($_REQUEST['filename']);
?>
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post by Galahad »

Thanks for the suggestion, I appreciate it. However, that worked just as well as my original script. It still doesn't work in IE on the actual server. It seems like if it is a server config difference, no browser would work on the actual server. And if it is a browser problem, IE wouldn't work on either server. Any other suggestions?
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post by Galahad »

Ok, I did some packet sniffing. After the request, the server sends what seem to be some default headers. The only difference is that the actual server (the one giving me problems) sends a "Connection: close" that my local machine (the one that works) does not send.

I tried to copy the /etc/php.ini, /etc/httpd/conf/httpd.conf, and /etc/httpd/conf.d/*.conf (not ssl.conf though, I'm not using a secure connection so it shouldn't matter) files from the server to my local machine. After changing a few small things (like the document root), I got apache running on my machine with the server's conf files. However, it still worked. It seems that the problem is not in any of those files. Any ideas why the server would send the "Connection: close" header?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Have a look at this thread.

I believe you need to change the

Code: Select all

<?php
header("Cache-Control: public\n"); 
?>
to

Code: Select all

<?php
header("Cache-Control: private"); 
?>
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post by Galahad »

Sorry, I should have changed that to read:

Code: Select all

<?php
header("Cache-Control: no-cache\n");
?>
I had just changed that because of some comments I read on the php manual page for header(), and I guess I hadn't changed it back before I posted.

We figured it out. The actual server was using ssl, but my local machine is not. I guess IE handles ssl connections a little weird or something. Anyway, I changed the link to the download page to be unsecure (not too bad, nothing but an id gets transfered there) and it works fine.
Post Reply