Jump to content

Need help with php-force download


epley

Recommended Posts

Hey guys, cant seem to get this to work on Safari.  Works great on FireFox and IE.  Anywho, the problem here is that with safari the file is created on the server and no download occurs.

 

// Open file export.csv.

$file = "infotrack_data.csv";

$f = fopen ($file,'w');

$filename = "infotrack_data.csv";

 

// Put all values from $out and $head to infotrack_data.csv.

fputs($f, $head);

fputs($f, $out);

fclose($f);

 

$file_size = filesize($file);

 

header('Expires: 0');

header('Pragma: public');

header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

header("Cache-Control: private",false);

header('Accept-Ranges: bytes');

header("Content-Length: $file_size");

header('Content-Type: Application/octet-stream');

header('Content-Disposition: attachment; filename="' . basename($filename). '"');

header('Connection: close');

 

readfile('infotrack_data.csv');

exit();

 

 

I'm sure its because I'm pretty new to this stuff.  I figure its because my file path isn't what I expect, but then that doesn't explain why I am able to download it with Firefox and IE.

 

Thanks,

 

Epley

Link to comment
https://forums.phpfreaks.com/topic/107893-need-help-with-php-force-download/
Share on other sites

$filename = '../my/image.gif';

header("Pragma: public");
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($filename).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));

@readfile($filename);
exit(0);

After trying out a number of force-download scripts, I found this one was the most reliable:

 

http://w-shadow.com/blog/2007/08/12/how-to-force-file-download-with-php/

 

However, I must admit I have never tried it on safari. But seeing as it has worked very well with IE and FF, in multiple situations, I'd say it's worth a try.

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.