0

I use this while loop to fetch the file;

<?php 
while(!feof($fp)){ //get this file with feof but dont tell file location on the server 
$buffer =  fread($fp,$sizechunk);
echo $buffer; 
ob_flush();
flush();
}

But feof is not good for large downloads. Because I'm serving large files and the download must be complete before it can exit the while loop. Exit it from the loop is very simple, but I need to exit the loop and the download will continue. Or I need to pull it with another code. Any idea?

Since the file stays in the loop, PHP seems to be used continuously and if I can't remove it, PHP will crash after the number of downloads is 6+.

I tried the following server-side changes;

In PHP settings:

max_execution_time = 60 to 3600

max_input_time = 30 to 7200

max_input_vars = 30 to 7200

post_max_size = 200M to 200000M

memory_limit = 2000M to 30000M

sub process = 20 to 200
BroserBros
  • 17
  • 6
  • You can use readFile() to avoid the loop. But the bottom line is that if you're serving the content via PHP then you cannot end the PHP script until you've served all the content. – ADyson Mar 31 '22 at 11:54
  • It's not clear what kind of crash you're experiencing though, or why the magic number is 6 - maybe PHP only has access to that number of threads, or runs out of memory or something...you didn't provide much detail on the actual problem. – ADyson Mar 31 '22 at 11:55
  • @ADyson I tried many definitions such as PHP subprocess, I changed the apache settings and no matter what I do, PHP only starts 6 downloads at the same time. Here my question for this: https://stackoverflow.com/questions/71667623/what-is-the-setting-in-php-that-enables-the-connection-limit – BroserBros Mar 31 '22 at 12:01
  • How are you triggering these downloads? If they're simultaneous AJAX requests then it may be a browser limitation which prevents it from triggering multiple simultaneous requests to the same URL or domain – ADyson Mar 31 '22 at 12:19
  • https://stackoverflow.com/questions/55015472/concurrent-ajax-requests-in-chrome could be relevant, for example. It would be good if you could investigate some more to see if that's actually what's going on. – ADyson Mar 31 '22 at 12:20
  • I thought to myself, but to prove it didn't happen, I started 6 downloads from my own computer and went to the same URL from another computer. The result was the same. The URL was pending. Requests is force download. If you want the try script: https://l24.im/RfM37e – BroserBros Mar 31 '22 at 12:24
  • I'm not going to download a multiple-GB movie from a random person on the internet to my machine, sorry. Put it on youtube or something if need be. – ADyson Mar 31 '22 at 12:26
  • Yep I can, but how u test my script on Youtube :) Thanks for helping, kind regards. – BroserBros Mar 31 '22 at 12:29
  • Well sorry I thought it was a video you sent to watch the proof? – ADyson Mar 31 '22 at 12:30
  • oh no, it was just for you to start 6 downloads and see what happens. You don't need to download it completely. But if you don't want to do that, I understand. – BroserBros Mar 31 '22 at 12:36
  • Well the file was an mkv which is a video – ADyson Mar 31 '22 at 12:37
  • You mean you want me to write some code to send AJAX requests to that URL? – ADyson Mar 31 '22 at 12:38
  • Why don't you provide the code you're already using for making the requests, then we can potentially reproduce the exact scenario. – ADyson Mar 31 '22 at 12:39
  • Even if this is not the core of your question: be warned that `"mslug = '{$file}'"` looks vulnerable for SQL injection. Please have a look at prepared statements to avoid getting hacked – Nico Haase Mar 31 '22 at 12:51
  • Thanks @NicoHaase im going to fix it after this problem. And ADyson, Now I have added the whole script, but that's it. It takes its main power from mybb base (global.php) and mysql. The main source of the problem is definitely while feof. But you can examine it. By the way, I tried your first suggestion, readFile(). The same error happened, it kept the php running again until the download finished. – BroserBros Mar 31 '22 at 12:56
  • Yes I knew it would. That was to prove my point that it's not the loop or feof which is the issue. If you stop the PHP script, then obviously the downloading will have to stop too, because there would be nothing generating the output to the client. You can't escape from that fact. But the first step to try and resolve/improve this is to verify precisely what is causing the 6-download limit - whether it's client side, or some limitation in the webserver, or in PHP settings, or just a memory limit or what. There's nothing in the info in your question yet which really helps to narrow that down. – ADyson Mar 31 '22 at 13:01
  • I see you've updated the PHP script, but that's just the server-side code (which sends the data back to the cilent in response to the download request). What I asked for was the _client-side_ code which is sending these multiple simultaneous download requests to your server and your PHP. Is it done via AJAX? Or are you just manually clicking on lots of links in a browser or something? – ADyson Mar 31 '22 at 13:01
  • The client-side sending requests at the same time is actually users. So when 6 users start downloading, nothing happens even if you click it. So you can think of it as clicking multiple times in a browser. – BroserBros Mar 31 '22 at 13:09
  • Actually, you're right, the problem is very broad and to narrow it down, I'll edit and write down what I did that didn't work. – BroserBros Mar 31 '22 at 13:12

0 Answers0