Jump to content

Saving a file from URL


kuwala

Recommended Posts

Ive been trying to save youtube video.flv files from a url like http://www.youtube.com/get_video?video_id=2FN1jExBUhw&sk=5ZAx5CJYDkJkXVHQLBLPnl9erb295YQXC&t=vjVQa1PpcFPiR9baKBgXvqJtbhJiIH4uRIHGRPEtzEk%3D

 

But for some reason the file is always 0 bytes. when you type the url in a browser it lets you save the video.flv file. I was thinking I may need to use cURL to do this. I have tried these two methods. But all i get is a 0k file. Any tips. Thanks.

 

<?php

/*

$contents = file_get_contents('http://www.youtube.com/get_video?video_id=2FN1jExBUhw&sk=5ZAx5CJYDkJkXVHQLBLPnl9erb295YQXC&t=vjVQa1PpcFPiR9baKBgXvqJtbhJiIH4uRIHGRPEtzEk%3D');

 

$fp = fopen('uploads/video.flv', 'w');

fwrite($fp, $contents);

fclose($fp);

 

 

 

$ch = curl_init("http://www.youtube.com/get_video?video_id=2FN1jExBUhw&sk=5ZAx5CJYDkJkXVHQLBLPnl9erb295YQXC&t=vjVQa1PpcFPiR9baKBgXvqJtbhJiIH4uRIHGRPEtzEk%3D");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

$output = curl_exec($ch);

 

$fh = fopen("uploads/video2.flv", 'w');

fwrite($fh, $output);

fclose($fh);

*/

 

 

$ch = curl_init();

/**

* Set the URL of the page or file to download.

*/

curl_setopt($ch, CURLOPT_URL,

'http://www.youtube.com/get_video?video_id=2FN1jExBUhw&sk=5ZAx5CJYDkJkXVHQLBLPnl9erb295YQXC&t=vjVQa1PpcFPiR9baKBgXvqJtbhJiIH4uRIHGRPEtzEk%3D');

/**

* Create a new file

*/

$fp = fopen('video3.flv', 'w');

/**

* Ask cURL to write the contents to a file

*/

curl_setopt($ch, CURLOPT_FILE, $fp);

/**

* Execute the cURL session

*/

curl_exec ($ch);

/**

* Close cURL session and file

*/

curl_close ($ch);

fclose($fp);

 

echo "kinnda done";

?>

Link to comment
https://forums.phpfreaks.com/topic/171847-saving-a-file-from-url/
Share on other sites

The video is around 20mbs I'd assume? Change your max execution time to 200 seconds and see if it does this again, but here's an example youtube php downloaders source code you could look into....

 

http://www.sizlopedia.com/wp-content/uploads/files/youtube-download.txt

etc.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.