Search code examples
phpiismimedmg

When serving .dmg files from PHP, user is getting 'disk image not recognized'


I have a .dmg file on my IIS server. When downloading directly the file opens just fine, but when I serve the file via PHP like so

$mime_types['dmg']   ='application/x-apple-diskimage';
$filename = getfile($_GET['dc']);           
$fakename = fakefilename($_GET['dc']);
$extension = fileexten($filename);
    if(($filename!= false)&&($fakename!=false&& @fopen($filename,'r')==true)){
    $mime = contenttype($extension);
    set_time_limit(0);
    header('Pragma: public');
    header('Expires: 0'); 
    header("Content-Type:".$mime);
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Content-Disposition: attachment;filename='.$fakename.'.'.$extension);
    header("Content-Transfer-Encoding: binary");
      if (ob_get_length() > 0) {
        ob_end_clean();
        }
    flush();
    @readfile($filename);
    }

I get an error on the mac saying 'disk image not recognized' I've also tried setting the .dmg application/octet-stream but I still run into the same issue.


Solution

  • My guess is that that this is either a case in which the content type is not set correctly or in which the content length is incorrectly set. Check to see if $mime = contenttype($extension); returns the correct content type.

    It would be useful to debug this with a web debugging proxy tool (like Fiddler or Charles) and post the entire response header, when accessing the file directly & when you access it via your PHP script.

    UPDATE (based on the comments below):

    The script had additional line breaks at the end of the file, which were being sent out in the response.