Re: Header problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Andrea Giammarchi wrote on 2009-10-03 13:40:
> Do you want users download the file or the zip?

They can choose between the two.

> do you send other headers before the download?

Header must come first (before you output anything) or you get a parse error, so I'm not sure what you mean here? You can see the source of the class at the testpage I linked to.

> It's quite a common error to set a default header in PHP at the beginning of whatever application, while header should be used as last exit point and never in the middle, or at the beginning, of a response.

I'm not sure what you mean by this? My download.php first checks for the relevant data is there (track_id/version_id, a session with member_id), then if the download is allowed (by access or if it has already been downloaded) then it fetches the relevant files, zip these to the disk and first then... creates the zip header and afterwards uses a readfile on the zipfile just created.

> Moreover, if you use readfile and then zip what do you expect, multiple downloads in one? This is not how HTTP work ... so please show more php, or explain better what you would like to do.

What do you need to see besides the testpage?

If I have 4 mp3 files of 5mb each, these will be zipped into one 20mb file, and that is of course only one download. That's the wish from the siteowner: easier download of several mixes of the same artist/track

I hope this makes the problem more clear to you :-)

I've debugged some more into the problem, this is a header of a zip download where the site works as expected:

HTTP/1.x 200 OK
Date: Sat, 03 Oct 2009 22:26:45 GMT
Server: Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.6 with Suhosin-Patch mod_ruby/1.2.6 Ruby/1.8.6(2007-09-24) mod_ssl/2.2.8 OpenSSL/0.9.8g
X-Powered-By: PHP/5.2.4-2ubuntu5.6
Accept-Ranges: bytes
Content-Length: 7083675
Content-Disposition: attachment; filename="Maxwell - Bad Habits (Remixes) (index.php=get_zip).zip"
Content-Transfer-Encoding: binary
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: application/zip

I call the file like this: index.php?get_zip=test_output.zip

index.php does this:

  $new_zipfile .= $_SESSION['download_title'].".zip";
/*
  header("Cache-Control: no-cache");
  header("Pragma: no-cache");
  header("Expires: now");
*/
  header('Accept-Ranges: bytes');
  header("Content-Type: application/zip");
  header("Content-Length: ".filesize($archiveName));
  header("Content-Disposition: attachment; filename=\"$new_zipfile\"");
  header("Content-Transfer-Encoding: binary");

  readfile("$archiveName");
  exit;

The outcommented headers was put in because I discovered that the download then was cached after giving the file a strange name, that name never occured in the download :-/ I've ended up by making a workaround by adding &nocache=".microtime()." to the get_zip link but then the site freezes againg during the download, arrrghh...

And headers from the original zip solution in download.php, headers are set in function.inc, this is the output from Live HTTP headers:

HTTP/1.x 200 OK
Date: Sat, 03 Oct 2009 21:41:49 GMT
Server: Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.6 with Suhosin-Patch mod_ruby/1.2.6 Ruby/1.8.6(2007-09-24) mod_ssl/2.2.8 OpenSSL/0.9.8g
X-Powered-By: PHP/5.2.4-2ubuntu5.6
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Accept-Ranges: bytes
Content-Length: 35756585
Content-Disposition: attachment; filename="Maxwell - Bad Habits (Remixes).zip"
Content-Transfer-Encoding: binary
Keep-Alive: timeout=15, max=98
Connection: Keep-Alive
Content-Type: application/zip

difference to the returned zip headers in index.php is pragma, cache-control and expires, so I've removed these headers from the function, but they still show up:

HTTP/1.x 200 OK
Date: Sat, 03 Oct 2009 22:25:36 GMT
Server: Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.6 with Suhosin-Patch mod_ruby/1.2.6 Ruby/1.8.6(2007-09-24) mod_ssl/2.2.8 OpenSSL/0.9.8g
X-Powered-By: PHP/5.2.4-2ubuntu5.6
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Accept-Ranges: bytes
Content-Length: 7083675
Content-Disposition: attachment; filename="Maxwell - Bad Habits (Remixes).zip"
Content-Transfer-Encoding: binary
Keep-Alive: timeout=15, max=96
Connection: Keep-Alive
Content-Type: application/zip

So I think my problem is cache related in some way.

Kind regards
Kim

 > Date: Sat, 3 Oct 2009 13:30:38 +0200
 > From: php.net@xxxxxxx
 > To: php-general@xxxxxxxxxxxxx
 > Subject:  Header problem
 >
 > Hi PHP people
 >
 > I have a really strange and annoying problem. I've got a site, where
 > members can download music. User clicks index.php (in index.php
 > there's an iframe, that opens another file), if certain check are okay
 > then a popup window opens download.php, where a mp3 file is fetched
 > from the server and renamed in the header, then pushed to the enduser,
 > this works fine. But now I want to create zipfiles too but when a user
 > downloads a zipfile it's like the whole site is freezed until download
 > has completed. My guess is that this is some sort of header problem
 > (see headers below), due to three headers at the same time, cause the
 > class works as expected in the test page i've created. Inputs to
 > correct headers would be appriciated very much :-)
 >
 > Mp3 headers:
 > $new_filename = "attachment; filename=\"{$artist} - {$title}.mp3\"";
 > header('Content-Description: File Transfer');
 > header("Content-Type: application/octet-stream");
 > header("Content-Length: $size");
 > header("Content-Disposition: $new_filename");
 > header("Content-Transfer-Encoding: binary");
 > readfile($source_file);
 >
 > Zip headers:
 > $zip = new zipfile();
 > $zip->add_dir(".");
 > $new_filename= "{$artist} - {$title}.mp3";
 > if(mysql_num_rows($result)) {
 > $zip->add_file($file, $new_filename);
 > }
 > header("Pragma: public");
 > header("Expires: 0");
 > header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
 > header("Cache-Control: private",false);
 > header("Content-type: application/zip");
 > #header("Content-Type: application/octet-stream");
 > header("Content-disposition: attachment; filename=\"zipTest.zip\"");
 > header('Content-Transfer-Encoding: binary');
 > ob_end_clean();
 > echo $zip->file();
 >
 > Code example: http://lps.netlinq.dk/test010/test_zip.class.php
 >
 > Headers (fetched with firefox add-on: live http headers)
 >
 > This is headers from the site, where the problem occurs:
 >
 > 1. click on the link to a title (Maxwell in this case)
 > ----------------------------------------------------------
 > http://lps.netlinq.dk/?action=download&track_id=357
 >
 > GET /?action=download&track_id=357 HTTP/1.1
 > Host: lps.netlinq.dk
 > User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.2) Gecko/
 > 20090803 Ubuntu/9.04 (jaunty) Shiretoko/3.5.2
 > Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/
 > *;q=0.8
 > Accept-Language: en-us,en;q=0.5
 > Accept-Encoding: gzip,deflate
 > Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
 > Keep-Alive: 300
 > Connection: keep-alive
 > Referer: http://lps.netlinq.dk/?action=download&track_id=350
 > Cookie: login_email=kim%40emax.dk;
 > PHPSESSID=fbb5d6adec802766cf6f638c99ab4f1d
 >
 > HTTP/1.x 200 OK
 > Date: Fri, 02 Oct 2009 15:15:21 GMT
 > Server: Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.6 with Suhosin-Patch
 > mod_ruby/1.2.6 Ruby/1.8.6(2007-09-24) mod_ssl/2.2.8 OpenSSL/0.9.8g
 > X-Powered-By: PHP/5.2.4-2ubuntu5.6
 > Expires: Thu, 19 Nov 1981 08:52:00 GMT
 > Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-
 > check=0
 > Pragma: no-cache
 > Vary: Accept-Encoding
 > Content-Encoding: gzip
 > Content-Length: 4250
 > Keep-Alive: timeout=15, max=100
 > Connection: Keep-Alive
 > Content-Type: text/html
 >
 > 2. I click on "download zip" (this is a link to index.php)
 > if conditions are met, then a popup with download.php is activated and
 > here a zip header is made
 >
 > ----------------------------------------------------------
 > http://lps.netlinq.dk/index.php
 >
 > POST /index.php HTTP/1.1
 > Host: lps.netlinq.dk
 > User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.2) Gecko/
 > 20090803 Ubuntu/9.04 (jaunty) Shiretoko/3.5.2
 > Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/
 > *;q=0.8
 > Accept-Language: en-us,en;q=0.5
 > Accept-Encoding: gzip,deflate
 > Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
 > Keep-Alive: 300
 > Connection: keep-alive
 > Referer: http://lps.netlinq.dk/?action=download&track_id=357
 > Cookie: login_email=kim%40emax.dk;
 > PHPSESSID=fbb5d6adec802766cf6f638c99ab4f1d
 > Content-Type: application/x-www-form-urlencoded
 > Content-Length: 131
 > action=ask_questions&download_zipfile=1&version_id
 > %5B1065%5D=1&version_id%5B1066%5D=1&version_id%5B1067%5D=1&version_id
 > %5B1068%5D=1
 >
 > HTTP/1.x 200 OK
 > Date: Fri, 02 Oct 2009 15:15:29 GMT
 > Server: Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.6 with Suhosin-Patch
 > mod_ruby/1.2.6 Ruby/1.8.6(2007-09-24) mod_ssl/2.2.8 OpenSSL/0.9.8g
 > X-Powered-By: PHP/5.2.4-2ubuntu5.6
 > Expires: Thu, 19 Nov 1981 08:52:00 GMT
 > Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-
 > check=0
 > Pragma: no-cache
 > Vary: Accept-Encoding
 > Content-Encoding: gzip
 > Content-Length: 3216
 > Keep-Alive: timeout=15, max=99
 > Connection: Keep-Alive
 > Content-Type: text/html
 > ----------------------------------------------------------
> http://lps.netlinq.dk/download.php?track_id=357&member_id=1&string=41e0cd250ca3a40598e2019fd4c813cc&kbit=320&zipfile=1
 >
 > GET /download.php?
> track_id=357&member_id=1&string=41e0cd250ca3a40598e2019fd4c813cc&kbit=320&zipfile=1
 > HTTP/1.1
 > Host: lps.netlinq.dk
 > User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.2) Gecko/
 > 20090803 Ubuntu/9.04 (jaunty) Shiretoko/3.5.2
 > Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/
 > *;q=0.8
 > Accept-Language: en-us,en;q=0.5
 > Accept-Encoding: gzip,deflate
 > Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
 > Keep-Alive: 300
 > Connection: keep-alive
 > Referer: http://lps.netlinq.dk/index.php
 > Cookie: login_email=kim%40emax.dk;
 > PHPSESSID=fbb5d6adec802766cf6f638c99ab4f1d
 >
 > HTTP/1.x 200 OK
 > Date: Fri, 02 Oct 2009 15:15:30 GMT
 > Server: Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.6 with Suhosin-Patch
 > mod_ruby/1.2.6 Ruby/1.8.6(2007-09-24) mod_ssl/2.2.8 OpenSSL/0.9.8g
 > X-Powered-By: PHP/5.2.4-2ubuntu5.6
 > Expires: Thu, 19 Nov 1981 08:52:00 GMT
 > Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-
 > check=0
 > Pragma: no-cache
 > Content-Disposition: attachment; filename="Maxwell - Bad Habits
 > (Remixes).zip"
 > Content-Transfer-Encoding: binary
 > Content-Length: 54234978
 > Keep-Alive: timeout=15, max=98
 > Connection: Keep-Alive
 > Content-Type: application/zip
 > ----------------------------------------------------------
 > 3. as long as the zip file is downloading the site "freezes" until
 > download is complete, then the link I've clicked is "activated"
> ___________________________________________________________________________
 >
 > And this is the headers from the test page:
 > http://lps.netlinq.dk/test010/test_zip.class.php
 > http://home.emax.dk/~emax/test/test_zip.class.php
 >
 > Same files, different servers
 >
 > headers from test010:
 >
 > http://home.emax.dk/~emax/test/test_zip.class.php?zip_to_browser=1
 >
 > GET /~emax/test/test_zip.class.php?zip_to_browser=1 HTTP/1.1
 > Host: home.emax.dk
 > User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.2) Gecko/
 > 20090803 Ubuntu/9.04 (jaunty) Shiretoko/3.5.2
 > Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/
 > *;q=0.8
 > Accept-Language: en-us,en;q=0.5
 > Accept-Encoding: gzip,deflate
 > Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
 > Keep-Alive: 300
 > Connection: keep-alive
 > Referer: http://home.emax.dk/~emax/test/test_zip.class.php?test=15
 >
 > HTTP/1.x 200 OK
 > Date: Fri, 02 Oct 2009 14:31:08 GMT
 > Server: Apache/1.3.37 (Unix) PHP/4.4.4
 > X-Powered-By: PHP/4.4.4
 > Pragma: public
 > Expires: 0
 > Cache-Control: must-revalidate, post-check=0, pre-check=0, private
 > Content-Disposition: attachment; filename="zipTest2.zip"
 > Content-Transfer-Encoding: binary
 > Keep-Alive: timeout=15, max=99
 > Connection: Keep-Alive
 > Transfer-Encoding: chunked
 > Content-Type: application/zip
 > ____________________________________________________________________
 >
 > headers from Netlinq:
 >
 > GET /test010/test_zip.class.php?zip_to_browser=1 HTTP/1.1
 > Host: lps.netlinq.dk
 > User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.2) Gecko/
 > 20090803 Ubuntu/9.04 (jaunty) Shiretoko/3.5.2
 > Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/
 > *;q=0.8
 > Accept-Language: en-us,en;q=0.5
 > Accept-Encoding: gzip,deflate
 > Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
 > Keep-Alive: 300
 > Connection: keep-alive
 > Referer: http://lps.netlinq.dk/test010/test_zip.class.php?test=21
 > Cookie: login_email=kim%40emax.dk;
 > PHPSESSID=fbb5d6adec802766cf6f638c99ab4f1d
 >
 > HTTP/1.x 200 OK
 > Date: Fri, 02 Oct 2009 14:55:37 GMT
 > Server: Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.6 with Suhosin-Patch
 > mod_ruby/1.2.6 Ruby/1.8.6(2007-09-24) mod_ssl/2.2.8 OpenSSL/0.9.8g
 > X-Powered-By: PHP/5.2.4-2ubuntu5.6
 > Pragma: public
 > Expires: 0
 > Cache-Control: must-revalidate, post-check=0, pre-check=0, private
 > Content-Disposition: attachment; filename="zipTest2.zip"
 > Content-Transfer-Encoding: binary
 > Keep-Alive: timeout=15, max=100
 > Connection: Keep-Alive
 > Transfer-Encoding: chunked
 > Content-Type: application/zip
 > ____________________________________________________________________
 >
 > --
 > PHP General Mailing List (http://www.php.net/)
 > To unsubscribe, visit: http://www.php.net/unsub.php
 >

------------------------------------------------------------------------
Windows Live: Friends get your Flickr, Yelp, and Digg updates when they e-mail you. <http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_3:092010>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux