2

I have tried to download file from server using below code but it not downloaded. It shows only content in browserIn image there was only code, not any file ..so can you please help me

<?php

if(file_exists($zipName))
{

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=\'Uttam_Solanki.zip\'');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($zipName));
    readfile($zipName);
    exit;

}

?>
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
codever
  • 35
  • 7
  • try adding .zip to the url end – Shank Jul 26 '16 at 18:58
  • @Shank: irrelevant. the browser only uses filename extensions in urls to GUESS what the type might be if the content-type header isn't present. most likely OP has "headers already sent" warnings, and is running with debug output disabled. – Marc B Jul 26 '16 at 19:01

3 Answers3

3

you need to change role in .htaccess

add application/octet-stream zip

Neelam Prajapati
  • 3,764
  • 3
  • 28
  • 62
1

your content-type must be `application/zip

here is a similar approach:

send zip file to browser / force direct download

Community
  • 1
  • 1
Miad Abrin
  • 952
  • 7
  • 14
  • that just tells the browser it's a zip file. if you want to force a download, you want to use a mime-type that's NOT likely to have a handler on the user's computer, and/or use a content-disposition header. – Marc B Jul 26 '16 at 19:01
  • I have tried Content-type:application/zip but not working – codever Jul 26 '16 at 19:02
  • here is the answer http://stackoverflow.com/questions/7470846/send-zip-file-to-browser-force-direct-download . you seriously have issues downvoting what you don't understand – Miad Abrin Jul 26 '16 at 19:07
  • i have tried above same questions method but i only content in browser like given in link – codever Jul 26 '16 at 19:17
  • can you send the headers you receive from browser? – Miad Abrin Jul 26 '16 at 19:18
  • @MiadAbrin my header is Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Encoding gzip, deflate Accept-Language en-US,en;q=0.5 Cache-Control no-cache Connection keep-alive Host Pragma no-cache Referer User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0 – codever Jul 26 '16 at 19:29
  • I mean the response headers – Miad Abrin Jul 26 '16 at 19:31
  • @MiadAbrin keep-alive Content-Length 329673 Content-Type text/html Date Tue, 26 Jul 2016 19:44:47 GMT Keep-Alive timeout=30 Server Apache/2 X-Powered-By PHP/5.5.22 – codever Jul 26 '16 at 19:45
  • As you can see your content type is not correct. I think you should debug you code for any notices or warnings and check you apache rules – Miad Abrin Jul 26 '16 at 19:49
0

How are you triggering this script?

If by link, maybe using download attribute would help?

Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76