download - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of download.php

  1. <?php
  2. if (isset($_POST['download'])) {
  3.     $video_url = $_POST['video_url'];
  4.  
  5.     function getVideoId($url) {
  6.         $pattern = '/(?:youtube\.com\/(?:[^\/]+\/[^\/]+\?.*v=|[^\/]+\/(?:v\/|embed\/|watch\?.*v=)))|youtu.be\/)([a-zA-Z0-9_\-]{11})/';
  7.         preg_match($pattern, $url, $matches);
  8.         return $matches[1];
  9.     }
  10.  
  11.     function constructDownloadUrl($videoId) {
  12.         return "https://www.youtube.com/watch?v=" . $videoId;
  13.     }
  14.  
  15.     // Validate the YouTube URL here (optional)
  16.  
  17.     // Extract the video ID from the URL
  18.     $video_id = getVideoId($video_url);
  19.  
  20.     // Construct the download URL
  21.     $download_url = constructDownloadUrl($video_id);
  22.  
  23.     // Redirect to the download URL
  24.     header("Location: $download_url");
  25.     exit();
  26. }
  27. ?>
  28.  
  29. <!-- Continue with the rest of the HTML code -->
  30.  
  31.  
File Description
  • download
  • PHP Code
  • 19 May-2023
  • 833 Bytes
You can Share it: