YouTube-dl server direct file download - php

I have succesfully installed youtube-dl on my server and from command line everything is working like a charm.
Now I want to be able to call a link on my site from my web browser which directly initiates a download of the file. So in that order:
Open site in browser (for example http://example.com/download.php?v=as43asx3
YouTube-dl processes the input
Web-browser downloads file
Temporary files will be deleted from server
I am not very experienced with this, but nevertheless need to solve this issue.

There might be a better way of doing this and I would appreciate seeing it, however, here is how I solved it:
<?php
ignore_user_abort(true);
//getting ID from URL
$youtubeID = $_GET['videoID'] ;
//getting audio file from youtube video via youtube-dl
exec('~/bin/youtube-dl --verbose --extract-audio -o "~/html/YouTubeDownloader/%(id)s.%(ext)s" '.$youtubeID);
//downloading file to client/browser
$filename = $youtubeID.".m4a";
header("Content-disposition: attachment;filename=$filename");
header("Content-type: audio/m4a");
readfile($filename);
//deleting file again
unlink($filename);?>

Related

My php script to download a file is just displaying it in browser when code is running in cloud, yet works fine on my local server

I am trying to download a file using php script. The file is in a database. I extracted the file content and I am printing it out with a header to download.
It works fine on my localhost but when code is running in the cloud, it then displays the file in the browser instead of downloading it.
I did search all similar cases, and I am doing what the best solutions recommend.
Here is my code:
$fcontent = $row->content;
header("Content-Type:application/octet-stream");
header("Pragma:public");
header("Content-Description:File Transfer");
header("Content-Transfer-Encoding:Binary");
header("Content-Disposition:attachment;filename=\"$fname\"");
header("Content-Length:" . $fsize);
header("Expires:0");
header("Cache-Control:must-revalidate");
ob_clean();
flush();
print ($fcontent);
die();

Lauching program via command in php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php How do I start an external program running - Having trouble with system and exec
how to open exe with php?
I had this idea and make hard to success it for several years,but failed at last. any one tell me a success method to do the job ?
<?php
if(isset($_POST['file_path'])){
/* -------
using "notepad++.exe" to open "test.php" file.
or run a bat file which calling "notepad++.exe" to open "test.php" file.
how to seting php.ini or firefox or any setting to do this job.
it is only for conveniently developing web page in my PC ,not for web servers
------- */
}
?>
<form action="test.php" method="post">
<input type="text" name="file_path" value="test.php"/>
<button type="submit">open with notepad++</button>
</form>
This would create something like:
To launch a program on the computer which runs the webserver:
<?php
exec('"C:\Program Files (x86)\Notepad++\notepad++.exe" "C:\foo.php"');
The above will work on vista/win7 IF the webserver does not run as a windows service. For example, if you run apache and it automatically starts when your computer boots, you probably installed it as a service. You can check to see if apache shows up in the windows services tab/thingy.
If the webserver runs as a service, you'll need to look into enabling the "allow desktop interaction" option for the service. But otherwise:
An easy test using php's new built in webserver(php 5.4+). The key thing here is you manually start the server from a shell, so it runs as your user instead of as a service.
<?php
// C:\my\htdocs\script.php
exec('"C:\Program Files (x86)\Notepad++\notepad++.exe" "C:\foo.php"');
start a webserver via a command window
C:\path\to\php.exe -S localhost:8000 -t C:\my\htdocs
Then in your browser
http://localhost:8000/script.php
I assume you are wanting the client device to open Notepad++ not the remote server. If this is the case, the best you can do is to serve up the file with the proper file type header and hope the client has Notepad ++ set up as the default application to open such a file.
Something like this should do it:
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="' . $file_name . '"'); // forces file download
header('Content-length: ' . filesize($file_path));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); // make sure file is re-validated each time it is requested
$fh = fopen($file_path, 'r');
while(!feof($fh)) {
$buffer = fread($fh, 2048);
echo $buffer;
}
fclose($fh);
Where $file_name is the name of the file (not the full path) and $file_path is the full path to the file
the finally successful way I tested.
thank Charles ,refer to php How do I start an external program running - Having trouble with system and exec
Start->Run, type "services.msc" to bring up Services control (other ways to get there, this is easiest IMO)
Locate your Apache service (mine was called "wampapache" using WampServer 2.0)
Open the service properties (double-click or right click->properties)
Flip to the Log On account and ensure the checkbox titled "Allow service to interact with Desktop" is checked
Flip back to the General tab, stop the service, start the service
then: in php
pclose(popen("start /B \"d:\\green soft\\notepad++5.8.4\\notepad++.exe\" \"d:\\PHPnow-1.5.6\\htdocs\\laji\\a.php\"", "r"));
Thank all your good guys, what a great help . I had finally made my idea to be true. Happy new year !
never had a reason to do so, but have you tried passthru() ?
http://php.net/manual/en/function.passthru.php
EDIT:
sorry the OP was really unclear at first glance..
what I would do is parse the file into a string or whatnot, then force the browser to treat that as a download.. php is server sided so you can`t only ask the browser to do some stuff..
$someText = 'some text here';
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="text.txt"');
echo $someText;

Download the YouTube video file directly to user's computer through browser

<?php
$youtubeUrl = "https://www.youtube.com/watch?v=Ko2JcxecV2E";
$content = json_encode ($file = shell_exec("youtube-dl.exe $youtubeUrl "));
$input_string =$content;
$regex_pattern = "/Destination:(.*.mp4)/";
$boolean = preg_match($regex_pattern, $input_string, $matches_out);
$extracted_string=$matches_out[0];
$file =explode(': ',$extracted_string,2)[1];
// Quick check to verify that the file exists
if( !file_exists($file) ) die("File not found");
// Force the download
header("Content-Disposition: attachment; filename=\"$file\"" );
header("Content-Length: " . filesize($file));
header("Content-Type: application/octet-stream;");
readfile($file);
?>
When I run this file the respective YouTube video is first downloaded to the localhost server folder where this PHP file is, using youtube-dl.exe and then it is pushed from that folder to browser download (forced download).
How to directly start the download to user's browser?
Also the file is running fine on localhost but not on remote server.
First, you need to use a version of youtube-dl for a platform of your webserver. The youtube-dl.exe is a build for Windows, while most webhostings use Linux.
Then use the passthru PHP function to run the youtube-dl with the -o - command-line parameter. The parameters makes youtube-dl output the downloaded video to its standard output, while the passthru passes the standard output to a browser.
You also need to output the headers before the the passthru. Note that you cannot know the download size in this case.
header("Content-Disposition: attachment; filename=\"...\"" );
header("Content-Type: application/octet-stream");
passthru("youtube-dl -o - $youtubeUrl");
If you need a video metadata (like a filename), you can run the youtube-dl first with the -j command-line parameter to get the JSON data without downloading the video.
Also you need 1) Python interpreter on the web server 2) to be able to use the passthru function 3) connectivity to YouTube from the PHP scripts. All these are commonly restricted on webhostings.
The trouble is probably within: shell_exec("youtube-dl.exe $youtubeUrl ")
Firstly, some hosts disable shell_exec for security reasons.
Secondly, youtube-dl.exe looks like it is probably a windows script, where as your remote server is probably Linux based.

can download file in localhost but not server

Anyone face this problem before?
like i implement a backup php script.
when i click the button download.
the file will auto download and store in my localhost folder.
but when i upload the script to server and try to run, the script can run and display the successful message. but no any file download.
//SAVE THE BACKUP AS SQL FILE
$handle = fopen($DbName.'-Database-Backup-'.$table.date('Y-m-d #h-i-s').'.sql','w+');
fwrite($handle,$data);
fclose($handle);
below is the script that i wrote.
Php script
Try using the following header in php.
Example:
header('Content-disposition: attachment; filename=my.sql');
header('Content-type: text/plain');

How can I start a Windows GUI program using PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php How do I start an external program running - Having trouble with system and exec
how to open exe with php?
I had this idea and make hard to success it for several years,but failed at last. any one tell me a success method to do the job ?
<?php
if(isset($_POST['file_path'])){
/* -------
using "notepad++.exe" to open "test.php" file.
or run a bat file which calling "notepad++.exe" to open "test.php" file.
how to seting php.ini or firefox or any setting to do this job.
it is only for conveniently developing web page in my PC ,not for web servers
------- */
}
?>
<form action="test.php" method="post">
<input type="text" name="file_path" value="test.php"/>
<button type="submit">open with notepad++</button>
</form>
This would create something like:
To launch a program on the computer which runs the webserver:
<?php
exec('"C:\Program Files (x86)\Notepad++\notepad++.exe" "C:\foo.php"');
The above will work on vista/win7 IF the webserver does not run as a windows service. For example, if you run apache and it automatically starts when your computer boots, you probably installed it as a service. You can check to see if apache shows up in the windows services tab/thingy.
If the webserver runs as a service, you'll need to look into enabling the "allow desktop interaction" option for the service. But otherwise:
An easy test using php's new built in webserver(php 5.4+). The key thing here is you manually start the server from a shell, so it runs as your user instead of as a service.
<?php
// C:\my\htdocs\script.php
exec('"C:\Program Files (x86)\Notepad++\notepad++.exe" "C:\foo.php"');
start a webserver via a command window
C:\path\to\php.exe -S localhost:8000 -t C:\my\htdocs
Then in your browser
http://localhost:8000/script.php
I assume you are wanting the client device to open Notepad++ not the remote server. If this is the case, the best you can do is to serve up the file with the proper file type header and hope the client has Notepad ++ set up as the default application to open such a file.
Something like this should do it:
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="' . $file_name . '"'); // forces file download
header('Content-length: ' . filesize($file_path));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); // make sure file is re-validated each time it is requested
$fh = fopen($file_path, 'r');
while(!feof($fh)) {
$buffer = fread($fh, 2048);
echo $buffer;
}
fclose($fh);
Where $file_name is the name of the file (not the full path) and $file_path is the full path to the file
the finally successful way I tested.
thank Charles ,refer to php How do I start an external program running - Having trouble with system and exec
Start->Run, type "services.msc" to bring up Services control (other ways to get there, this is easiest IMO)
Locate your Apache service (mine was called "wampapache" using WampServer 2.0)
Open the service properties (double-click or right click->properties)
Flip to the Log On account and ensure the checkbox titled "Allow service to interact with Desktop" is checked
Flip back to the General tab, stop the service, start the service
then: in php
pclose(popen("start /B \"d:\\green soft\\notepad++5.8.4\\notepad++.exe\" \"d:\\PHPnow-1.5.6\\htdocs\\laji\\a.php\"", "r"));
Thank all your good guys, what a great help . I had finally made my idea to be true. Happy new year !
never had a reason to do so, but have you tried passthru() ?
http://php.net/manual/en/function.passthru.php
EDIT:
sorry the OP was really unclear at first glance..
what I would do is parse the file into a string or whatnot, then force the browser to treat that as a download.. php is server sided so you can`t only ask the browser to do some stuff..
$someText = 'some text here';
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="text.txt"');
echo $someText;

Categories