0

So I was trying to download a zipfile I previously created in PHP but my browser just echo's out a huge string with many different and weird letters.

Anyway here is my php code for downloading my php:

header("Content-type: application/zip");
        header("Content-Disposition: attachment; filename='/var/www/html/1/' . $usernumber . '/download.zip'");
        header("Content-length: " . filesize('/var/www/html/1/' . $usernumber . '/download.zip'));
        header("Pragma: no-cache"); 
        header("Expires: 0");
        readfile('/var/www/html/1/' . $usernumber . '/download.zip');

My mission is to make the browser download the file and not to display it like its doing now.

I am pretty new to php and backend programming - please have mercy :)

Thanks

userlip
  • 79
  • 1
  • 2
  • 11
  • The `filename=` value should be wrapped in `"` not `'` and also you don't have to specify a full path but simply the base file name. – Havenard Sep 17 '16 at 04:47
  • Make sure you write your headers properly aswell, `Content-length` is not a valid HTTP header, its `Content-Length`, with upper case on first letters. – Havenard Sep 17 '16 at 04:49
  • 1
    @Havenard, http headers are case insensitive. – Devon Bessemer Sep 17 '16 at 04:50
  • @Devon Can't confirm or deny, what I know is that this is how it's written on every HTTP application ever, and from experience some HTTP clients will have issues if you deviate from that, specially on cell phones. – Havenard Sep 17 '16 at 04:54
  • 1
    @Havenard No major browser should have any issues with it as it's the RFC standard. http://stackoverflow.com/questions/5258977/are-http-headers-case-sensitive There is nothing in the standard that suggests or recommends caps at all. – Devon Bessemer Sep 17 '16 at 04:56

1 Answers1

0

Try:

header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');

I always use this for my zip files, to force the download (without displaying it).

Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49