1

Am trying to add file download to my site for audio video and pdf files Am using the php header() function but instead of downloading the output file, it keeps on downloading the php page instead. here is my code.

    header('Content-disposition: attachment; 
            Content-type: audio/mpeg; 
            filename=thefile.type');
Daniel Barde
  • 2,603
  • 5
  • 31
  • 40

2 Answers2

0

You need to write the contents of the file out, e.g.

header('Content-disposition: attachment; 
   Content-type: audio/mpeg; 
   filename=thefile.type');
echo file_get_contents("thefile.type");
tdlive aw'sum
  • 400
  • 1
  • 7
0

This works pretty well

    if (file_exists("audio/thefile.type"))
    {
         if (FALSE!== ($handler = fopen('thefile.type', 'r')))
            {
             header('Content-Description: File Transfer');
             header('Content-Type: application/octet-stream');
             header('Content-Disposition: attachment; 
                     filename=audio/thefile.type');
             header('Content-Transfer-Encoding: chunked'); //changed to chunked
             header('Expires: 0');
             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
             header('Pragma: public');
             //header('Content-Length: ' . filesize('thefile.type')); //Remove

             //Send the content in chunks
             while(false !== ($chunk = fread($handler,4096)))
                  {
                    echo $chunk;
                  }
             }
             exit;
    }
    else{

           echo "<h1>Content error</h1><p>The file does not exist!</p>";
         }
Daniel Barde
  • 2,603
  • 5
  • 31
  • 40