Downloading MP3 and MP4 files using PHP











up vote
0
down vote

favorite












I am making a downloader for mp3 and mp4 and i want two php files in php with 2 buttons calling both php files but only the first php file works.



code for 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>


code for downloadmp3.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;
}
}
?>


and code for downloadmp4.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;
}
}
?>


Updated:
Form submitted for MP3 form works.
But form submitted for MP4 doesn't works.
Please provide an solution to make it work.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I am making a downloader for mp3 and mp4 and i want two php files in php with 2 buttons calling both php files but only the first php file works.



    code for 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>


    code for downloadmp3.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;
    }
    }
    ?>


    and code for downloadmp4.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;
    }
    }
    ?>


    Updated:
    Form submitted for MP3 form works.
    But form submitted for MP4 doesn't works.
    Please provide an solution to make it work.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am making a downloader for mp3 and mp4 and i want two php files in php with 2 buttons calling both php files but only the first php file works.



      code for 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>


      code for downloadmp3.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;
      }
      }
      ?>


      and code for downloadmp4.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;
      }
      }
      ?>


      Updated:
      Form submitted for MP3 form works.
      But form submitted for MP4 doesn't works.
      Please provide an solution to make it work.










      share|improve this question















      I am making a downloader for mp3 and mp4 and i want two php files in php with 2 buttons calling both php files but only the first php file works.



      code for 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>


      code for downloadmp3.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;
      }
      }
      ?>


      and code for downloadmp4.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;
      }
      }
      ?>


      Updated:
      Form submitted for MP3 form works.
      But form submitted for MP4 doesn't works.
      Please provide an solution to make it work.







      php html download include mp3






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 11 '14 at 11:42









      brimble2010

      11.9k52240




      11.9k52240










      asked Jan 27 '14 at 16:45









      Rene

      4,47831223




      4,47831223
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          Only first php file calls are working because of the following if condition:



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


          Since downloadmp3.php has been included first therefore it's if condition is giving the result.
          Use proper form handler to make it work.
          Also the best practice is to use unique names for the forms.






          share|improve this answer




























            up vote
            0
            down vote













            This code is working fine for me in all the browsers and the downloaded file is playing good :
            download-audio.php



            <?php
            $file = $_GET['file'];
            header ('Content-type: octet/stream');
            header ('Content-disposition: attachment; filename='.$file.';');
            header('Content-Length: '.filesize($file));
            readfile($file);
            exit;
            ?>

            test.html

            <a href='download-audio.php?file=duetsong.mp3'>Download Duet song MP3</a>
            <a href='download-audio.php?file=duetsong.mp4'>Download Duet song MP4</a>





            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f21386537%2fdownloading-mp3-and-mp4-files-using-php%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote













              Only first php file calls are working because of the following if condition:



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


              Since downloadmp3.php has been included first therefore it's if condition is giving the result.
              Use proper form handler to make it work.
              Also the best practice is to use unique names for the forms.






              share|improve this answer

























                up vote
                0
                down vote













                Only first php file calls are working because of the following if condition:



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


                Since downloadmp3.php has been included first therefore it's if condition is giving the result.
                Use proper form handler to make it work.
                Also the best practice is to use unique names for the forms.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Only first php file calls are working because of the following if condition:



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


                  Since downloadmp3.php has been included first therefore it's if condition is giving the result.
                  Use proper form handler to make it work.
                  Also the best practice is to use unique names for the forms.






                  share|improve this answer












                  Only first php file calls are working because of the following if condition:



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


                  Since downloadmp3.php has been included first therefore it's if condition is giving the result.
                  Use proper form handler to make it work.
                  Also the best practice is to use unique names for the forms.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 11 '14 at 11:49









                  Akhilesh

                  8771128




                  8771128
























                      up vote
                      0
                      down vote













                      This code is working fine for me in all the browsers and the downloaded file is playing good :
                      download-audio.php



                      <?php
                      $file = $_GET['file'];
                      header ('Content-type: octet/stream');
                      header ('Content-disposition: attachment; filename='.$file.';');
                      header('Content-Length: '.filesize($file));
                      readfile($file);
                      exit;
                      ?>

                      test.html

                      <a href='download-audio.php?file=duetsong.mp3'>Download Duet song MP3</a>
                      <a href='download-audio.php?file=duetsong.mp4'>Download Duet song MP4</a>





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        This code is working fine for me in all the browsers and the downloaded file is playing good :
                        download-audio.php



                        <?php
                        $file = $_GET['file'];
                        header ('Content-type: octet/stream');
                        header ('Content-disposition: attachment; filename='.$file.';');
                        header('Content-Length: '.filesize($file));
                        readfile($file);
                        exit;
                        ?>

                        test.html

                        <a href='download-audio.php?file=duetsong.mp3'>Download Duet song MP3</a>
                        <a href='download-audio.php?file=duetsong.mp4'>Download Duet song MP4</a>





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          This code is working fine for me in all the browsers and the downloaded file is playing good :
                          download-audio.php



                          <?php
                          $file = $_GET['file'];
                          header ('Content-type: octet/stream');
                          header ('Content-disposition: attachment; filename='.$file.';');
                          header('Content-Length: '.filesize($file));
                          readfile($file);
                          exit;
                          ?>

                          test.html

                          <a href='download-audio.php?file=duetsong.mp3'>Download Duet song MP3</a>
                          <a href='download-audio.php?file=duetsong.mp4'>Download Duet song MP4</a>





                          share|improve this answer












                          This code is working fine for me in all the browsers and the downloaded file is playing good :
                          download-audio.php



                          <?php
                          $file = $_GET['file'];
                          header ('Content-type: octet/stream');
                          header ('Content-disposition: attachment; filename='.$file.';');
                          header('Content-Length: '.filesize($file));
                          readfile($file);
                          exit;
                          ?>

                          test.html

                          <a href='download-audio.php?file=duetsong.mp3'>Download Duet song MP3</a>
                          <a href='download-audio.php?file=duetsong.mp4'>Download Duet song MP4</a>






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Sep 10 '16 at 0:15









                          Senthil

                          7591515




                          7591515






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f21386537%2fdownloading-mp3-and-mp4-files-using-php%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Xamarin.iOS Cant Deploy on Iphone

                              Glorious Revolution

                              Dulmage-Mendelsohn matrix decomposition in Python