0

I am trying to download a video file directly on my server using the bellow php-curl script, but it stops downloading after getting around 120mb of file, the files are more than 500mb and some of them are 1gb and 1.5gb in size. I searched a lot but got nothing to resolve. I am running on a shared hosting.

if ($url) {
    $file_loc = 'moviez/' . $name;
    $fp = fopen($file_loc, 'w+');
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 0);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
}
DJ22T
  • 1,628
  • 3
  • 34
  • 66
  • you should check the `upload_max_filesize` setting in your `php.ini`, see [here](http://stackoverflow.com/questions/2184513/php-change-the-maximum-upload-file-size) – celeriko May 14 '14 at 19:41
  • since m running on a shared hosting, i don't have permission to change this. I have also asked the administrator to increase the connection time but they denied and asked me to find any alternative idea. – Shivendra dubey May 14 '14 at 19:47
  • download shall be upload, since you are putting it "on the server" :) – DJ22T May 14 '14 at 19:48
  • well i don't have access to php.ini, also in simple word my problem is that m trying to save a video file to my server from youtube.com which is more than 1gb in size. But after fetching around 100mb's my script returns this 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'." – Shivendra dubey May 14 '14 at 20:25

3 Answers3

0

I suspect that maybe the script is timing out. The default is 30 seconds if I remember correctly. Your host may be limiting your script run time as well. You can use ini_set('max_execution_time', nnn) to increase the timeout.

Edit: Better yet, use set_time_limit(), which will throw an error:

Warning: set_time_limit(): Cannot set time limit in safe mode

if your host is limiting you.

Luke
  • 768
  • 2
  • 14
  • 33
0

As Luke said, your script is timing out before your download completes. The problem with setting max_execution_time is that only affects uploads.

Your solution should be handled by readfile() or file_get_contents(). Great source is found here: http://www.ibm.com/developerworks/library/os-php-readfiles/index.html?ca=drs

Edit: Minor confusion between Max_execution_time and max_input_time.

Edit Two: Example,

<?php
$file = $_GET['file'];
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
?>


<a href="direct_download.php?file=batman.mkv">Download the batman</a>
Rob Steiner
  • 125
  • 1
  • 10
  • but this will start download on browser side, instead i want to save the content on server. – Shivendra dubey May 14 '14 at 20:07
  • Ohhh, I was confused by the word "download" in your question! You're looking to upload to your server. You will be looking at max_input_time in your php.ini. Here: http://www.php.net/manual/en/info.configuration.php#ini.max-input-time – Rob Steiner May 14 '14 at 20:12
  • well i don't have access to php.ini, also in simple word my problem is that m trying to save a video file to my server from youtube.com which is more than 1gb in size. But after fetching around 100mb's my script returns this 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'." – Shivendra dubey May 14 '14 at 20:26
  • That now sounds like an max_execution_time problem because the script would be continuously retrieving the file. Without access to the php.ini file, I am not entirely certain how to proceed. Sorry, bud. – Rob Steiner May 14 '14 at 20:32
0

Check this script. It worked well for me to download file larger than 500mb.

Light Weight PHP Script To Download Remote File To Local Server

<?php
// maximum execution time in seconds
set_time_limit (24 * 60 * 60);
if (!isset($_POST['submit'])) die();
// folder to save downloaded files to. must end with slash
$destination_folder = 'files/';
$url = $_POST['url'];
$newfname = $destination_folder . basename($url);
$file = fopen ($url, "rb");
if ($file) {
  $newf = fopen ($newfname, "wb");
if ($newf)
  while(!feof($file)) {
    fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
  }
}
if ($file) {
  fclose($file);
}
if ($newf) {
  fclose($newf);
}
?>
Sanyam Jain
  • 99
  • 1
  • 3