I'm having no luck serving a .dmg from my online store. I stripped down the code to just the following to debug, but no matter what I get a zero-byte file served:
header('Content-Type: application/x-apple-diskimage'); // also tried octet-stream
header('Content-Disposition: attachment; filename="My Cool Image.dmg"');
$size = filesize('/var/www/mypath/My Cool Image.dmg');
header('Content-Length: '.$size);
readfile('/var/www/mypath/My Cool Image.dmg');
This same code works for a number of other file types that I serve: bin, zip, pdf. Any suggestions? Professor Google is not my friend.
Found the solution. The culprit was readfile() and may have been memory related. In place of the readfile() line, I'm using this:
$fd = fopen ('/var/www/mypath/My Cool Image.dmg', "r");
while(!feof($fd)) {
set_time_limit(30);
echo fread($fd, 4096);
flush();
}
fclose ($fd);
It's now serving all filetypes properly, DMG included.
You should not have spaces in the filename (Spaces should not be used when it comes to web hosted files)
Try something like this or rename your file with no spaces:
<?php
$path ='/var/www/mypath/';
$filename = 'My Cool Image.dmg';
$outfile = preg_replace('/[^a-zA-Z0-9.-]/s', '_', $filename);
header('Content-Type: application/x-apple-diskimage'); // also tried octet-stream
header('Content-Disposition: attachment; filename="'.$outfile.'"');
header('Content-Length: '.sprintf("%u", filesize($file)));
readfile($path.$filename); //This part is using the real name with spaces so it still may not work
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With