0

I'm trying to download a file from a remote server with file_put_contents. This script is called via ajax. The problem i'm having is the script timeout when the file is large e.g. (500mb). I get 504 Gateway Timeout - nginx

download.php $destination = "/home/mywebsite/public_html/wp-content/channels/videos/test.mp4";

    $link = "http://www.example.com/videos/movie.mp4"; //500mb
    $result = file_put_contents($destination, fopen($link, 'r'));

I'm using dedicated hosting. I've changed my php.ini and confirmed in phpinfo();

max_execution_time 7200 max_input_time 7200 max_input_vars 1000 memory_limit -1 output_buffering 4096 post_max_size 1200M upload_max_filesize 1000M

This script keeps timing out. Is there another solution how do i solve? When i check the directory the file is successfully downloaded but the page times out. So i can't return any data via ajax.

How do i solve?

mdnba50
  • 369
  • 4
  • 23
  • instead i would suggest you to execute wget from php exex – M A SIDDIQUI Apr 20 '17 at 08:40
  • If you are downloading the file on your server and it is not being served to anyone in that request there is no need for PHP to be there in the first place. You can simply do `wget http://www.example.com/videos/movie.mp4` if you have *NIX – Hanky Panky Apr 20 '17 at 08:40

2 Answers2

1

You should also change nginx fcgi timeout values. PHP script continues executing but your connection between nginx and PHP timeouts.

Omer A
  • 11
  • 1
1

Make download asynchronous. Like one process only fill some DB or rabbitMq with download requests and other wil cosume it (maybe cron)

  • This is the most sensible option for long-running processes. It's also fairly good protection against DOS attacks, as an attempt would just generate a bunch of request entries in your database (which you could easily block by putting a limit on how many entries a specific user can have at once) rather than bringing your server down by flooding it with long-running processes. – Niet the Dark Absol Apr 20 '17 at 08:41
  • will the cron script timeout? – mdnba50 Apr 20 '17 at 08:50
  • It may but that timeout you can override by `ini_set('max_execution_time', 0);` in php code without any changes in nginx (whitch is not maintained by you..just asuming) – Martin Kuchyňár Apr 20 '17 at 09:00