Download YouTube video in server - php

I have created a YouTube search engine + download + MP3 convert script. I have used Jeckman's YouTube Downloader for creating this script. Everything is OK, except, I want to download the video into the server instead it downloading it to my computer. I want to do this because, after it gets download, I shall convert the video to MP3 using FFmpeg.
Is there any way to get the video downloaded in my server instead of my computer?
The download.php contains the following code:
<?php
// Check download token
if (empty($_GET['mime']) OR empty($_GET['token']))
{
exit('Invalid download token 8{');
}
// Set operation params
$mime = filter_var($_GET['mime']);
$ext = str_replace(array('/', 'x-'), '', strstr($mime, '/'));
$url = base64_decode(filter_var($_GET['token']));
$name = urldecode($_GET['title']). '.' .$ext;
// Fetch and serve
if ($url)
{
// Generate the server headers
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
{
header('Content-Type: "' . $mime . '"');
header('Content-Disposition: attachment; filename="' . $name . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
}
else
{
header('Content-Type: "' . $mime . '"');
header('Content-Disposition: attachment; filename="' . $name . '"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
}
readfile($url);
exit;
}
// Not found
exit('File not found 8{');
?>

I've found out a solution to store the YouTube file to server. I have removed the header stuff and put $download_video_file = file_put_contents($file_path, fopen($url, 'r')); in place of readfile($url); and it worked like a magic! ^_^
Here's the complete code:
<?php
// Check download token
if (empty($_GET['mime']) OR empty($_GET['token']))
{
exit('Invalid download token 8{');
}
// Set operation params
$mime = filter_var($_GET['mime']);
$ext = str_replace(array('/', 'x-'), '', strstr($mime, '/'));
$url = base64_decode(filter_var($_GET['token']));
$name = urldecode($_GET['title']). '.' .$ext;
// Fetch and serve
if ($url)
{
// Generate the server headers
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
{/*
header('Content-Type: "' . $mime . '"');
header('Content-Disposition: attachment; filename="' . $name . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
*/}
else
{/*
header('Content-Type: "' . $mime . '"');
header('Content-Disposition: attachment; filename="' . $name . '"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
*/}
$download_video_file = file_put_contents($file_path, fopen($url, 'r'));
exit;
}
// Not found
exit('File not found 8{');
?>

Related

PHP content disposition file name error safari

I have a database from which I would like the user to download data
I'm trying to download mp3 files. If the file contains, for example, Cyrillic characters, then in Safari I get the file ÐÐ¸Ð·Ð½ÐµÑ lite - ÐÑÐ°Ñ talk.mp3, in other browsers I get a normal file name Бизнес lite - Краш talk.mp3. Here is a sample code. Help, please, what am I doing wrong?
`
$src_file = ROOT_PATH . '/files/uploads/' . substr( $track_file['name'], 0, 2) . '/' . $track_file['name'];
if(file_exists($src_file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'. $track_file['title'] . '.mp3"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($src_file));
ob_clean();
flush();
readfile($src_file);
exit;
`
would use if else
if user agent safari . you can find browser name by the $_SERVER['HTTP_USER_AGENT']
$src_file = ROOT_PATH . '/files/uploads/' . substr( $track_file['name'], 0, 2) . '/' . $track_file['name'];
if(file_exists($src_file)) {
if($useragent=='safari'){
header('content-type: application/octet-stream');
header('content-Disposition: attachment; filename='.$src_file);
header('Pragma: no-cache');
header('Expires: 0');
readfile($src_file);
}else{
//your current code
}

Why download file from php only 1kb?

I try to force download a pdf file from php that are in server...but all the file that i download only 1kb size.It not the same with actual size do i need to declare file size before download?
<?php
$path = "C:\Users\omamu02\Desktop\TESTPRINT" ;
$file = "NMT PRV PHG 370 2017.pdf";
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-disposition: attachment; filename= $file"); //Tell the filename to the browser
header("Content-type: application/force-download");//Get and show report format
header("Content-Transfer-Encoding: binary");
header("Accept-Ranges: bytes");
readfile($path); //Read and stream the file
get_curret_user();
error_reporting(0);
?>
First of all, you should fix your filename= $file header. Wrap your $file variable with a ' chars at least. Also, you don't need the closing tag in the end of the PHP file.
And I'm not sure about your headers, so I suggest you try the function below, it's quite common for any type of data and already contains some bugfixes and workarounds:
function download_file($file_path, $file_name = null, $file_type = 'application/octet-stream')
{
if ($file_name === null)
{
$file_name = basename($file_path);
}
if (file_exists($file_path))
{
#set_time_limit(0);
header('Content-Description: File Transfer');
header('Content-Type: ' . $file_type);
header('Content-Disposition: attachment; filename="' . str_replace('"', "'", $file_name) . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file_path));
readfile($file_path);
}
exit;
}

Resume Download Links in PHP

The following file (which I don't understand it at all) serves my requested file and makes it ready to be downloaded. But, the links that are created and served by this file miss really important items when downloading by IDM.
File size: (unknown), Time left (unknown) , Resume capability (unknown).
Very unprofessional and bad in all aspects. What should I do as a novice?
if (isset($status) && ($status == 'd')) { //if Every Thing Is Fine The Download Start
$filename = $url;
$extension = fileexten($filename);
$fakename = fakefilename($downcode);
$mime = contenttype($extension);
// set_time_limit(0);
if ($notadownload == FALSE) {
header('Pragma: public');
header('Expires: 0');
header("Content-Type:" . $mime);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Disposition: attachment;filename=' . $fakename . '.' . $extension);
header("Content-Transfer-Encoding: binary");
ob_clean();
flush();
#readfile($filename);
} else {
$mime = getContentType($filename);
header("Content-Type:" . $mime);
if (strpos($mime, 'text') == FALSE) {
$extension = fileexten($filename);
header('Content-Disposition: inline;filename=' . $fakename . '.' . $extension);
}
echo #file_get_contents($filename);
}
}

Youtube video download working fine on localhost but not working on live (Production) server

I am working on a php file, where i can put youtube video url, it gives me the download link, when i click on that download button on localhost, video starts downloading, but when i put it on live, it does not work.
This is code for button
echo ' ' . '<b>Record MP4</b>';
Here is the code behind that button download button
include_once('config.php');
// Check download token
if (empty($_GET['mime']) OR empty($_GET['token']))
{
exit('Invalid download token 8{');
}
// Set operation params
$mime = filter_var($_GET['mime']);
$ext = str_replace(array('/', 'x-'), '', strstr($mime, '/'));
$url = base64_decode(filter_var($_GET['token']));
$name = urldecode($_GET['title']). '.' .$ext;
// Fetch and serve
if ($url)
{
$size=get_size($url);
// Generate the server headers
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
{
header('Content-Type: "' . $mime . '"');
header('Content-Disposition: attachment; filename="' . $name . '"');
header('Expires: 0');
header('Content-Length: '.$size);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
}
else
{
header('Content-Type: "' . $mime . '"');
header('Content-Disposition: attachment; filename="' . $name . '"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Content-Length: '.$size);
header('Pragma: no-cache');
}
readfile($url);
exit;
}
// Not found
exit('File not found 8{');
It always give me error that invalid download token, but on localhost, it works properly. Why it won't work when it is live?

Youtube downloader script on server end

I am trying to generate a script which can download youtube videos on the server, I would like the user to provide the video url.
I have searched a lot but, could not make it work. Here's my code.
<?php
// Check download token
if (empty($_GET['mime']) OR empty($_GET['token']))
{
exit('Invalid download token 8{');
}
// Set operation params
$mime = filter_var($_GET['mime']);
$ext = str_replace(array('/', 'x-'), '', strstr($mime, '/'));
$url = base64_decode(filter_var($_GET['token']));
$name = urldecode($_GET['title']). '.' .$ext;
// Fetch and serve
if ($url)
{
// Generate the server headers
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
{/*
header('Content-Type: "' . $mime . '"');
header('Content-Disposition: attachment; filename="' . $name . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
*/}
else
{/*
header('Content-Type: "' . $mime . '"');
header('Content-Disposition: attachment; filename="' . $name . '"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
*/}
$download_video_file = file_put_contents($file_path, fopen($url, 'r'));
exit;
}
// Not found
exit('File not found 8{');
?>
But I am still stuck at this, Please help me out.
You will get the code from here
Ref : https://code.google.com/p/php-youtube-downloader/
<?php
require_once('youtube.lib.php');
$download_link = get_youtube('http://www.youtube.com/watch?v=SAQZ0BDXn48');
// we will have array here, index 0 is the video ID and index 1 is the download link.
echo $download_link[1];
?>
http://php-youtube-downloader.googlecode.com/files/youtube_downloader.zip
GitHub : https://github.com/jeckman/YouTube-Downloader

Categories