0

Im working on my own website where the user can press a download button and download a music file. The user chooses file format as follows:

<div id="choose_format"><p>Select Download Format:</p>
    <select id='download_format' name="download_format">
        <option value="1">--SELECT--</option>
        <option value="m4r">M4r (Apple iPhone)</option>
        <option value="mp3">Mp3 (Android)</option>
    </select>
</div>

then they press a download button and this is the function that is called when a download button is clicked:

function download(song_id){
        var download_format = document.getElementById('download_format').value;
        if(download_format == '1'){
        alert("Please select a download format from the dropdown menu");
        $('#download_format').addClass('highlight_box');
        }else{

        var product_id = 'productID' + song_id;
        var productID = document.getElementById(product_id).value;

        window.location.href = "download_tone3.php?download_format=" + download_format + "&productID=" + productID;
        //xmlhttp_down = createXHR();
        //xmlhttp_down.onreadystatechange = down_callback;
        //xmlhttp_down.open("POST", "download_tone3.php" ,true);
        //xmlhttp_down.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        //xmlhttp_down.send("download_format=" + download_format + "&productID=" + productID);

        }


}

In download_tone3.php I want to connect to my music database so I can compare the product Id of the music file with the database to retrieve the right download link. So I have the following php file download_tone3.php:

<?php
require ("../../identity/music_connect.php");
$productID = $_REQUEST['productID'];
$download_format = $_REQUEST['download_format'];
$GLOBALS['download_link'] = '';
$result = mysqli_query($con,"SELECT * FROM products WHERE ProductID=" . $productID);
        if (!$result) {
            printf("Error: %s\n", mysqli_error($con));
            exit();
            }
while($row = mysqli_fetch_array($result)){  
    if($download_format == 'm4r'){
        $GLOBALS['download_link'] = $row['Productm4r'];
    }else if($download_format == 'mp3'){
        $GLOBALS['download_link'] = $row['Productmp3'];
    }
}

$mimeTypes = array(
    'mp3' => 'audio/mpeg',
    'm4r' => 'audio/x-m4r',
);
// set the file here (best of using a $_GET[])
$file = "../" . $GLOBALS['download_link'];

//gets the extension of the file to be loaded for searching array above
$ext = explode('.', $file);
$ext = end($ext);

// gets the file name to send to the browser to force download of file
$fileName = explode("/", $file);
$fileName = end($fileName);

// opens the file for reading and sends headers to browser
$fp = fopen($file,"r") ;
header("Content-Type: ".$mimeTypes[$ext]);
header('Content-Disposition: attachment; filename="'.$fileName.'"');

// reads file and send the raw code to browser     
while (! feof($fp)) {
        $buff = fread($fp,4096);
    echo $buff;
        //file_put_contents('media/audio/ringtones/anthem-sarah_monks.' . $download_format,$buff);
}
// closes file after whe have finished reading it

fclose($fp);

//mysqli_close($con);
?>

This php file worked great to force download a file until i tried to require another php file and work with the database. It throws an error: "Cannot modify header information - headers already sent by (output started at /home4/monksee/identity/music_connect.php:15)" Thanks for your help.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Sarah
  • 1,943
  • 2
  • 24
  • 39
  • You can try using `ob_start();` but that doesn't always work. – Funk Forty Niner Jun 02 '14 at 13:58
  • thanks I am reading the duplicate question mentioned by @david. I do have echo in my music connect file so this makes sense that that would be the reason. I will edit my files now. thanks – Sarah Jun 02 '14 at 14:00
  • 1
    Thanks everyone. I removed the "echo" and it still didn't work so then i checked for any white space and there was white space after the last ?> tag in my music_connect file. When i deleted this white space it worked. Thanks a lot – Sarah Jun 02 '14 at 14:11

1 Answers1

1

Your /music_connect.php is echo/print something. check it out.

volkinc
  • 2,143
  • 1
  • 15
  • 19