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
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 :) )

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.