Search code examples
phpcurlexecfopenfile-get-contents

PHP download from URL (can't download over 123MB)


Some days I have problem to download from URL. Many ways checked but I can't download URL file over 123MB!

My codes:

.htaccess

<IfModule php5_module>
   php_value allow_url_fopen On
   php_flag asp_tags Off
   php_flag display_errors Off
   php_value max_execution_time 10000
   php_value max_input_time 10000
   php_value max_input_vars 10000
   php_value memory_limit 12800M
   php_value upload_max_filesize 2000M
</IfModule>

functions.php

<?php

ini_set('memory_limit', '-1');
ini_set('upload_max_size' , '1024M' );
ini_set('post_max_size', '1024M');
ini_set('max_execution_time', '10000' );
set_time_limit (24 * 60 * 60);
//---------------------------------------------------------
function download_url_one($url , $filename)
{
    //first way
    file_put_contents($filename, fopen($url, 'r'));
}
//---------------------------------------------------------
function download_url_two($url , $filename , $post = null)
{
    //second way
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_REFERER, "https://www.youtube.com/");
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 200000); 
    curl_setopt($ch, 156 , 200000);//CURLOPT_CONNECTTIMEOUT_MS
    curl_setopt($ch, CURLOPT_TIMEOUT, 200000); //timeout in seconds
    curl_setopt($ch, 155, 200000);//CURLOPT_TIMEOUT_MS

    $result = curl_exec($ch);
    curl_close($ch);
    file_put_contents($filename, $result);
}
//---------------------------------------------------------
function download_url_three($url , $filename)
{
    //third way
    exec("wget $url -O $filename");
}
//---------------------------------------------------------
function download_url_four($url , $filename)
{
    //fourth way
    file_put_contents($filename, file_get_contents($url));
}
//---------------------------------------------------------

?>

index.php

<?php
include_once('functions.php');

$url = 'https://pc.tedcdn.com/talk/podcast/2004/None/DanGilbert_2004-480p.mp4';
download_url_one($url , 'example.mp4');
//download_url_two($url , 'example.mp4');
//download_url_three($url , 'example.mp4');
//download_url_four($url , 'example.mp4');
?>

This url file size is 143.76 MB. After use each functions for download url, only about ~123 MB download is done. and then show error 500 Internal Server Error. Request Timeout. This request takes too long to process, it is timed out by the server. If it should not be timed out, please contact administrator of this web site to increase 'Connection Timeout'.

For example:

  • Function download_url_one (128.02 MB downloaded)

  • Function download_url_two -> (122.54 MB downloaded)

  • Function download_url_three -> (128.02 MB downloaded)
  • Function download_url_four -> (124.45 MB downloaded)

I talked with my server support and apply the same settings to this picture. See image host config

Please help me, how to solve this problem?


Solution

  • This was a problem from the server and I talk support server again for increase time limit on server and solved. Time limit 120 seconds for default and when increase to 400 seconds, problem solved. I think when I set any things changed on PHP Select Version, this changes is upper of other settings. (more info) Thanks for @Halcyon.