Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Save as popup window has disappeared

    Hello all.

    I've made a little button to allow users to download mp3 files. It's just a hyperlink to the following php file, which I found on the internet to do this job:

    Code:
    <?php
    
    $mp3  = $_GET['mp3'];
    $file = 'recordings/'.$mp3;
    
    if(ini_get('zlib.output_compression')) {
      ini_set('zlib.output_compression', 'Off');
    }
    
    if ( file_exists($file) ) {
      header("Pragma: public");
      header('Expires: '.gmdate('D, d M Y H:i:s').' GMT');
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
      header("Cache-Control: private",false);
      header("Content-Type: application/mp3");
      header('Content-Disposition: attachment; filename="'.$mp3.'"');
      header("Content-Transfer-Encoding: binary");
      header("Content-Length: ".@filesize($file));
      set_time_limit(0);
      @readfile($file) OR die("<html><body OnLoad=\"javascript: alert('Unable to read file!');history.back();\" bgcolor=\"#F0F0F0\"></body></html>");
      exit;
    } else {
      die("<html><body OnLoad=\"javascript: alert('File not found!');history.back();\" bgcolor=\"#F0F0F0\"></body></html>");
    }
    
    ?>
    On my localhost it works fine; I click the button and the "Save as" dialogue window pops up. However, on my webserver it doesn't; instead it opens the mp3 file.

    Anybody got any suggestions?

    Thanks,

    Chris

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    The only thing I can think of that would explain what you have described is that the headers are sent prior to the headers for the attachment.
    Enable your error reporting:
    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    And run it on the host. Does it whine about the headers already sent? You may want to check that $_GET as well with:
    PHP Code:
    $mp3  = isset($_GET['mp3']) ? $_GET['mp3'] : ''
    Or just a suppressor to stop it from throwing possible errors as well.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    brichcja (11-28-2010)


  •  

    Posting Permissions

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