简体   繁体   中英

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.

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
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM