Create download link for music or video

后端 未结 2 1329
不知归路
不知归路 2020-12-05 20:11

i have html file with video and audio file. and i want to links file such as mp3 or mp4 using tag a href and download that file. My video and audio file stored in same folde

相关标签:
2条回答
  • 2020-12-05 20:31

    It depends of your browser settings and plugins however if you are using php you can do a script to download the file like this one:

    <?php   
    if (isset($_GET['file'])) { 
        $file = $_GET['file'] ;
            if (file_exists($file) && is_readable($file) && preg_match('/\.mp3$/',$file))  { 
                header('Content-type: application/mp3');  
                header("Content-Disposition: attachment; filename=\"$file\"");   
                readfile($file); 
            } 
        } else { 
        header("HTTP/1.0 404 Not Found"); 
        echo "<h1>Error 404: File Not Found: <br /><em>$file</em></h1>"; 
    } 
    ?>
    

    save it as download.php

    then create a link like this one

    <html>
    <body>
    <a href="download.php?file=test.mp3">download</a>
    </body>    
    </html>
    

    It should work now, have a nice day.

    0 讨论(0)
  • 2020-12-05 20:41

    You can try this. I've tried it, and it's working for me.

    <a href="link/to/your/download/file" download> Download link </a>
    
    0 讨论(0)
提交回复
热议问题