Forum Moderators: coopster

Message Too Old, No Replies

Help, changed server now script doesn't work

readfile header dumps to screen

         

babushka

12:44 am on May 3, 2008 (gmt 0)

10+ Year Member



I changed server and now the script below doesn't work. It should bring up the "SaveAs" dialog box but instead dumps the file the screen. Any ideas?
Thanks!

$cfile = $_cpath.$_cd;
$type = "application/zip";
$size = @filesize($cfile);
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Transfer-Encoding: binary");
set_time_limit(0);

header("Content-Disposition: attachment; filename=$_cd");
header("Content-length: $size");
header("Content-type: $type");
@readfile($cfile) or die("File not found.");
exit;

coopster

1:50 pm on May 3, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The browser determines whether the file will be viewed using a helper application or saved locally. This is a user setting and can even be told to download automatically to a predetermined local directory or prompt the user. Since this is a user setting, there isn't a whole lot you can do about it. See RFC2183 for proper syntax and some possible options.

Relative reading:
PDF downloads using readfile() [webmasterworld.com]
Download PDF documents [webmasterworld.com]
Annoying Header/File Name Problem [webmasterworld.com]
How prevent IE from adding [1] to name of a file passed by readfile()? [webmasterworld.com]

babushka

2:01 pm on May 3, 2008 (gmt 0)

10+ Year Member



Thanks, but before I changed servers, this code worked correctly. Now I'm hosting the script on a different server, and it doesn't work. I did not change my browser settings or the code. Just the server in which the script is hosted.

Any other ideas?

jatar_k

2:04 pm on May 3, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



where do the variables

$cfile
$size
$type

come from?

are they posted? is it just a register_globals issue?

seems odd but if it isn't working then you need to look at configuration issues. As coop showed, most of this is controlled by the browser.

though it also seems strange sometimes the code shouldn't have worked and did, a server change sometimes brings this out, I know it shouldn't make sense but it does as I've seen it many times and have not been able to explain some of them.

babushka

2:54 pm on May 3, 2008 (gmt 0)

10+ Year Member



I specifically define them in the script. I left the definitions out because I didn't think they were relevant. It works though on the other host provider.

I was on a shared host and now on my own VPS. Perhaps I have done something secure the VPS that causes this or it is a PHP configuration problem, I am not sure. I'm not even sure where to start to debug it.

The code btw is from the comments to the php site's "readfile" page.

Thanks for looking...

babushka

7:01 pm on May 3, 2008 (gmt 0)

10+ Year Member



Does anyone have any ideas what in my configuration would cause this or how to change the script to work with some unknown configuration difference?

coopster

7:15 pm on May 3, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would begin by comparing the version differences in PHP as well as having a look at the headers being returned by both servers to note the differences. Firefox has an extension called LiveHttpHeaders that is an invaluable tool.

babushka

8:03 pm on May 3, 2008 (gmt 0)

10+ Year Member



Thanks! This is what it shows. Nothing pops out at me.

-----------------------------------------------------

[mysite.com...]

POST /myfile.php HTTP/1.1
Host: mysite.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: [mysite.com...]
Cookie: __utma=105385238.43134625.1209773928.1209773928.1209844734.2; __utmz=105385238.1209773928.1.1.utmccn=(direct)¦utmcsr=(direct)¦utmcmd=(none); __utmb=105385238; __utmc=105385238
Content-Type: application/x-www-form-urlencoded
Content-Length: 35
agreeterms=on&seccode=9&_download=1
HTTP/1.x 200 OK
Date: Sat, 03 May 2008 19:59:08 GMT
Server: Apache/2.2.8 (Unix) mod_ssl/2.2.8 OpenSSL/0.9.8b mod_auth_passthrough/2.1 mod_bwlimited/1.4
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html
----------------------------------------------------------

babushka

8:45 pm on May 3, 2008 (gmt 0)

10+ Year Member



allow_url_fopen if 'on' on the old host and 'off' on the new host. Would that cause the file to dump as opposed to save as? I don't think so, but I'm not really sure.

bubbasheeko

4:42 am on May 4, 2008 (gmt 0)

10+ Year Member



You mentioned that zlib was being used in your script, make sure that your new host has that enabled in php. Check phpinfo() on your new server, you should see this module enabled in PHP:

zlib

ZLib Support enabled
Compiled Version 1.2.0.7
Linked Version 1.2.1.2

If you don't, I believe that would mess up the header enough to cause the problem you are seeing.

babushka

3:28 pm on May 4, 2008 (gmt 0)

10+ Year Member



Thanks for looking. I have PHPinfo turned off so that it can't be run for security reasons and I am not sure how to run it at the commandline. However, Cpanel shows the following zlib options:

Zlib zlib.output_compression Off
Zlib zlib.output_handler **DEFAULT**, Click to Edit

I'm assuming it is ok?

babushka

3:31 pm on May 4, 2008 (gmt 0)

10+ Year Member



Well I just changed output compression to 'On' and now it works. Are there any issues with me having that 'On'?

Thanks...

bubbasheeko

4:35 pm on May 4, 2008 (gmt 0)

10+ Year Member



Hi babushka,

On the security side of things, nothing that comes to mind.

Functionality, I don't think so...but I could be wrong. I have never specified anything for zlib when it came to creating a file to be downloaded.

babushka

5:07 pm on May 4, 2008 (gmt 0)

10+ Year Member



Thanks. Hope something doesn't pop up in the future and I forget I did this. lol