Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve a .dmg file via PHP/readfile?

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.

like image 495
CaymanCarver Avatar asked Apr 26 '12 04:04

CaymanCarver


2 Answers

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.

like image 196
CaymanCarver Avatar answered Nov 16 '22 19:11

CaymanCarver


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
?>
like image 1
Lawrence Cherone Avatar answered Nov 16 '22 19:11

Lawrence Cherone