SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    force download mp3

    I am trying to use the following script to allow visitors to download audio files (.wav, .mp3, etc.). All it produces is gibberish. Please advise. Here's what I have done:
    1) On the "Download: page.
    HTML Code:
    <p><a href="/force-download.php?file=streams/KDZ_Kou_Kou.mp3">Download</a></p>
    2) on force_download.php
    PHP Code:
               <?php
               
    //force-download_v1.php

    $filename $_GET['file'];

    // required for IE, otherwise Content-disposition is ignored
    if(ini_get('zlib.output_compression'))
      
    ini_set('zlib.output_compression''Off');

    // addition by Jorg Weske
    $file_extension strtolower(substr(strrchr($filename,"."),1));

    if( 
    $filename == "" 
    {
      echo 
    "<html>Download Now</title><body>ERROR: download file NOT SPECIFIED.</body></html>";
      exit;
    } elseif ( ! 
    file_exists$filename ) ) 
    {
      echo 
    "<html><title>Download Now</title><body>ERROR: File not found.</body></html>";
      exit;
    };
    switch( 
    $file_extension )
    {
      case 
    "pdf"$ctype="application/pdf"; break;
      case 
    "exe"$ctype="application/octet-stream"; break;
      case 
    "zip"$ctype="application/zip"; break;
      case 
    "doc"$ctype="application/msword"; break;
      case 
    "xls"$ctype="application/vnd.ms-excel"; break;
      case 
    "ppt"$ctype="application/vnd.ms-powerpoint"; break;
      case 
    "wav"$ctype="audio/wav"; break;
      case 
    "mp3"$ctype="audio/mp3"; break;  
      case 
    "gif"$ctype="image/gif"; break;
      case 
    "png"$ctype="image/png"; break;
      case 
    "jpeg":
      case 
    "jpg"$ctype="image/jpg"; break;
      default: 
    $ctype="application/force-download";
    }
    header("Pragma: public"); // required
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); // required for certain browsers 
    header("Content-Type: $ctype");
    // change, added quotes to allow spaces in filenames, by Rajkumar Singh
    header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($filename));
    readfile("$filename");
    exit();

    ?>
    Please advise.
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  2. #2
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is the result when you right click and choose save as? My guess is the browser you are using does not like the mime types you are using or the mime types are not being set correctly.

    Other than that the code looks fine at first glance. Good Luck

  3. #3
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    remove the space before the php tag
    PHP Code:
    here           <?php
    my mobile portal
    ghiris.ro


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •