How to serve .dmg file via PHP / readfile?

I was not lucky that I was in my online store .dmg. I canceled the code until the next to debug, but no matter what I get as a file with a zero byte:

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'); 

The same code works for a number of other types of files that I serve: bin, zip, pdf. Any suggestions? Google professor is not my friend.

+6
source share
2 answers

Found a solution. The culprit was readfile () and may have been memory related. Instead of the readfile () line, I use the following:

 $fd = fopen ('/var/www/mypath/My Cool Image.dmg', "r"); while(!feof($fd)) { set_time_limit(30); echo fread($fd, 4096); flush(); } fclose ($fd); 

Now it correctly serves all file types, including DMG.

+4
source

You should not have spaces in the file name (spaces should not be used when it comes to web files)

Try something like this or rename your file without 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 ?> 
+1
source

Source: https://habr.com/ru/post/914188/


All Articles