Jump to content

PHP headers to download mp3 in Safari (OSX)


richrock

Recommended Posts

I've got a script that works in pretty much everything else I've got, but Safari OSX.

 

The script is

 

<?php

    ini_set ("display_errors", "1");
    error_reporting(E_ALL);
    clearstatcache();

    $filename = $_GET['file'];
    // place this code inside a php file and call it f.e. "download.php"
$path = $_SERVER['DOCUMENT_ROOT']."/"; // change the path to fit your websites document structure
$fullPath = $path.$filename;

if ($fd = fopen ($fullPath, "r")) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);




    switch ($ext) {
        case "pdf":
        header("Content-type: application/pdf"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
        break;
        default;
        header("Content-type: application/octet-stream");
        //header("Content-type: audio/mpeg");
        header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    }
    header("Expires: 0");
    header("Last-Modified: " . gmdate ("D, d M Y H:i:s", filemtime ($filepath)) . " GMT");
    header("Content-length: $fsize");
    header("Pragma: public");
    //header("Cache-control: private"); //use this to open files directly
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Accept-Ranges: bytes");
    header("Connection: close");
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
}
fclose ($fd);
exit;
// example: place this kind of link into the document where the file download is offered:
// <a href="download.php?download_file=some_file.pdf">Download here</a>

?>

 

The result is an url that looks like this:

 

http://server.local:82/site_development/components/com_music/assets/zipit.php?file=/testfolder/music/Elgar%20Violin%20Concerto/Elgar%20Violin%20Concerto/02%20Violin%20Concerto.mp3

 

The urls are correct, the mp3 file is there.

 

And Safari on the mac seems absolutely bent on trying to play the file instead of downloading it.  PC versions of Safari work fine and download quite happily.  Is this something I have any control over, or has it been set by the end-user?

 

I changed the header set up based on an article I found which fixed the PC Safari/Chrome issues I had. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.