【问题标题】:Downloading MP3 and MP4 files using PHP使用 PHP 下载 MP3 和 MP4 文件
【发布时间】:2014-02-18 15:02:27
【问题描述】:

我正在为 mp3 和 mp4 制作一个下载器,我想要 php 中的两个 php 文件,两个按钮调用两个 php 文件,但只有第一个 php 文件有效。

popup.php 的代码

<?php
    include "downloadmp3.php";
    include "downloadmp4.php";
?>
<html>
<head></head>
<body bgcolor="#E6E6E6">
    <form method="post">
        <label for="url">Download mp3:</label>
        <input type="text" name="url" value="" id="url">
        <input type="submit" name="submit" value="Download">
        <hr>
    </form>
    <form method="post">
        <label for="url1">Download mp4:</label>
        <input type="text" name="url1" value="" id="url1">
        <input type="submit" name="submit" value="Download">
    </form>
</body>
</html>

下载mp3.php的代码

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    $url = (isset($_POST['url']) && !empty($_POST['url'])) ? $_POST['url'] : false;
    if (!$url) {
        echo "Vul alstublieft een url in";
    } else {
        $source = file_get_contents($url);
        $source = urldecode($source);

        // Verkrijg de video titel.
        $vTitle_results_1 = explode('<title>', $source);
        $vTitle_results_2 = explode('</title>', $vTitle_results_1[1]);

        $title = trim(str_replace(' – YouTube', ”, trim($vTitle_results_2[0])));

        // Extract video download URL.
        $dURL_results_1 = explode('url_encoded_fmt_stream_map', "url=", $source);
        $dURL_results_2 = explode('\u0026quality', $dURL_results_1[1]);

        // Force download van d  video.
        $file = str_replace(' ', '_', strtolower($title)).'.mp4';

        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=$file");
        header("Content-Type: video/mp4");
        header("Content-Transfer-Encoding: binary");

        readfile($dURL_results_2[0]);

        exit;
    }
}
?>

以及下载mp4.php的代码

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    $url = (isset($_POST['url1']) && !empty($_POST['url1'])) ? $_POST['url1'] : false;
    if (!$url) {
        echo "Vul alstublieft een url in";
    } else {
        $source = file_get_contents($url);
        $source = urldecode($source);

        // Verkrijg de video titel.
        $vTitle_results_1 = explode('<title>', $source);
        $vTitle_results_2 = explode('</title>', $vTitle_results_1[1]);

        $title = trim(str_replace(' – YouTube', ”, trim($vTitle_results_2[0])));

        // Extract video download URL.
        $dURL_results_1 = explode('url_encoded_fmt_stream_map', "url1=", $source);
        $dURL_results_2 = explode('\u0026quality', $dURL_results_1[1]);

        // Force download van d  video.
        $file = str_replace(' ', '_', strtolower($title)).'.mp4';

        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=$file");
        header("Content-Type: video/mp4");
        header("Content-Transfer-Encoding: binary");

        readfile($dURL_results_2[0]);

        exit;
    }
}
?>

更新: 为 MP3 表单作品提交的表单。 但是为 MP4 提交的表单不起作用。 请提供解决方案以使其正常工作。

【问题讨论】:

    标签: php html download include mp3


    【解决方案1】:

    由于以下 if 条件,只有第一个 php 文件调用有效:

    if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    

    由于 downloadmp3.php 已被首先包含,因此它的if condition 给出了结果。 使用适当的表单处理程序使其工作。 此外,最佳做法是为表单使用唯一名称。

    【讨论】:

      【解决方案2】:

      这段代码在所有浏览器中对我来说都很好,下载的文件也很好: 下载-audio.php

      测试.html 下载二重唱MP3 下载二重唱MP4

      【讨论】:

        猜你喜欢
        • 2023-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-04
        • 1970-01-01
        • 2020-04-24
        • 1970-01-01
        • 2016-07-12
        相关资源
        最近更新 更多