Jump to content

force download Safari and Chrome


Recommended Posts

Hi everyone !

 

First of all thanks for all the help in advance !

 

I'm trying to force the download of png files for a websites.

It works perfectly with Firefox but not with Safari (nothing happening when clicking) and Chrome (file corrupted).

I looked everywhere on internet for a fix but didn't find any working solutions !

Please find below my code :

 

ob_end_clean();
//header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
//header("Content-Type: application/download");
header("Content-Transfer-Encoding: Binary");
header('Accept-Ranges: bytes');
header ("Content-Disposition: attachment; filename=".basename($_REQUEST['path']).";");
header("Content-Description: File Transfer");
header('Expires: 0');
header('Pragma: public');
header("Content-Length: ".$filesize($_REQUEST['path']));
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Connection: close");
readfile($_REQUEST['path']);

 

Best,

Link to comment
https://forums.phpfreaks.com/topic/208944-force-download-safari-and-chrome/
Share on other sites

not sure why it would corrupt, but with the content disposition it SHOULD force download either way, you could try setting the content-type as image/png because google chrome has a weird database type security system, and it encrypts everything it receives, and if it receives it as something it isn't it could be causing the corruption, (the db structure  I'm referring to is it's cache, not sure why it would affect a download.. but, try it anyway doesn't hurt :) )

Thanks for the reply.

I had the same though and then tried this solution but same problem...

I think there is something wrong with the filesize because chrome force the download but the file is 0kb.

Again for Safari nothing happens at all.  :wtf:

very good catch.. you're using filesize as a variable :)

 

I know its possible to do that but in this case you'd want to drop the $ :)

 

EDIT: For anybody who looks @ this what I meant by filesize as a variable:

 

http://www.php.net/manual/en/functions.variable-functions.php

 

for example:

<?php

  $filePath = '....';

  $filesize = 'filesize';

  $filesize($filePath);

?>

 

but since you didn't specify the function to the variable it won't work lol

 

but disregard the above if you aren't interested <33

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.