How to implement Content-Disposition: attachment?

后端 未结 3 1440
我寻月下人不归
我寻月下人不归 2020-11-28 15:36

I am trying to make it so that mp3\'s on my site are downloaded by left clicking instead of having to right click and save as, So in order to do that, I have to set the Cont

相关标签:
3条回答
  • 2020-11-28 16:22

    Example of MP3 Lists:

    <a href="download.php?file=testing.mp3">Download MP3</a>
    <a href="download.php?file=testing2.mp3">Download MP3</a>
    

    download.php :

    <?php
    
    $file = $_GET['file'];  
    
    header('Content-type: audio/mpeg');
    
    header('Content-Disposition: attachment; filename="'.$file.'"');
    
    ?>
    
    0 讨论(0)
  • 2020-11-28 16:22

    There's a special function for that in PHP, too:

    bool http_send_content_disposition ( string $filename [, bool $inline = false ] )

    See PHP Manual here: http://de2.php.net/manual/en/function.http-send-content-disposition.php

    0 讨论(0)
  • 2020-11-28 16:26

    As others have said, you don't do that in HTML, and a dynamic solution (e.g., using PHP) is overkill.

    In your case, I'd configure the Content-Disposition header in the web server config. For Apache, you could set the header based on location, or have a .htaccess file that matches certain filenames.

    0 讨论(0)
提交回复
热议问题