r/programminghelp • • Mar 27 '21

PHP Very strange issue I am having with PHP

I found a tutorial on YouTube on how to download YouTube videos with PHP. I copied his source code into a .php file, but I get this when trying to run it.

https://imgur.com/a/R2AUW2O

Here is the code:

<!DOCTYPE html>

<html> <head> <title>Download Youtube Video</title> </head> <body> <form method="POST"> <h1>Enter the URL of the video you would like to download.</h1> <label>Url of Video:</label> <input type="text" name="url"> <input type="submit" name="submit"> </form> </body> </html>

<?php // Load and initialize downloader class if (isset($_POST["submit"])) { include_once 'Youtube.class.php'; $handler = new YouTubeDownloader(); // Youtube video url $youtubeURL = $_POST["url"]; // Check whether the url is valid if(!empty($youtubeURL) && !filter_var($youtubeURL, FILTER_VALIDATE_URL) === false){ // Get the downloader object $downloader = $handler->getDownloader($youtubeURL); // Set the url $downloader->setUrl($youtubeURL); // Validate the youtube video url if($downloader->hasVideo()){ // Get the video download link info $videoDownloadLink = $downloader->getVideoDownloadLink(); $videoTitle = $videoDownloadLink[0]['title']; $videoQuality = $videoDownloadLink[0]['quality']; $videoFormat = $videoDownloadLink[0]['format']; $videoFileName = strtolower(str_replace(' ', '_', $videoTitle)).'.'.$videoFormat; $downloadURL = $videoDownloadLink[0]['url']; // $fileName = preg_replace('/[^A-Za-z0-9._\-]/', '', basename($videoFileName)); if(!empty($downloadURL)){ // Define headers header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename=$fileName"); header("Content-Type: application/zip"); header("Content-Transfer-Encoding: binary"); // Read the file readfile($downloadURL); } }else{ echo "The video is not found, please check YouTube URL."; } }else{ echo "Please provide valid YouTube URL."; } } ?>

Anyone know why it is doing this?

4 Upvotes

4 comments sorted by

2

u/[deleted] Mar 27 '21

It looks like PHP isn't installed properly.

Make a file (test.php) and give it these contents, then see what it does in the browser.

<?php
phpinfo();
?>

1

u/GhostbusterJeffrey Mar 27 '21

It writes the code as text in HTML

1

u/[deleted] Mar 27 '21

Then you don't have PHP installed, or installed properly, or you're not putting the file in the right location to be served properly.

1

u/Luieka224 Mar 27 '21

I think your XAMPP, MAMP, or WAMP server is configured incorrectly.