How PHP copy () handles memory

I need to use the PHP copy() function to copy files from one place to another. I intentionally do not use rename() .

Files ranging in size from 500 MB to 1 GB. It seems that after I launched the PHP script, it takes about 10-20 seconds (one file is processed per execution).

My server refused to download these files due to the values max_execution_time , post_max_size , upload_max_filesize and memory_limit , all of which are set ridiculously high, but the server just shuts down when I tried to download.

Now I am worried that the server will fail if this copy() operation is started when the site is dealing with a lot of traffic.

So my question is: does PHP copy() in such a way as to overload server memory and / or deadlines?

I know that a PHP script requires a lot of time, but I hope that time is, in fact, just a period of "waiting" with low memory, when PHP sits back and allows the server OS to move the file ... I would not think that PHP will need to load the file into a buffer or something like that in order to copy it, but the discussion of memory at this level is a topic that is slightly different from my understanding.

Can someone explain how PHP copy() uses memory, and are there any risks associated with memory overloads?

+4
source share
1 answer

Look at the source for copy , it works in the long run by reading and writing 8 KB chunks - so it will not allocate the whole part of the server memory. This, of course, is what one would expect, as people have already commented.

However, several copies of large files running in parallel can easily kill the performance of the storage medium (especially if it is made from rust of rotation), which, in turn, can lead to absurd copies. This can easily turn out to be longer than the maximum time limit of the script, so it would be nice to remove the limit before starting the copy.

+2
source

Source: https://habr.com/ru/post/1499487/


All Articles