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. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.