user998163
user998163

Reputation: 483

Force File Download in Safari

I am trying to make a file downloadable in Safari. This is my code:

<?php
$file = $_GET['file'];
header ("Content-Disposition: attachment");
header ("Content-type: application/octet-stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
?>

It works fine in FF but in Safari I get a 0kb file.

I think I there is something wrong in the header I am sending. What could be the error?

Upvotes: 0

Views: 10284

Answers (1)

Waqar Alamgir
Waqar Alamgir

Reputation: 9978

Try This.

<?php
$file = $_GET['file'];
header('content-type: application/octet-stream');
header('content-Disposition: attachment; filename='.$file);
header('Pragma: no-cache');
header('Expires: 0');
readfile($file);
?>

Beware: Relying on $_GET['file'] without any sanitation or restrictions is very dangerous. Can be used to compromise the entire server application!

Upvotes: 5

Related Questions