Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does PHP copy() handle memory

I need to use PHP's copy() function to copy files from one location to another. I am intentionally not using rename().

The files are 500MB to 1GB. It seems that it takes about 10-20 seconds to move them after I start the PHP script (one file is handled per execution).

My server was rejecting the upload of these files because of max_execution_time, post_max_size, upload_max_filesize and memory_limit values, all of which are set ridiculously high, but the server would just time out when I was trying to upload.

Now I'm concerned the server will fail if this copy() operation is running while the site is dealing with a lot of traffic.

So my question is, does PHP's copy() operate in a way that would overload the server's memory and/or execution time limits?

I know that the PHP script takes a lot of time to complete, but my hope is that the time is essentially just a period of low-memory "waiting time" as PHP sits back and lets the server OS move the file... I wouldn't think that PHP would need to load the file into a buffer or anything like that in order to copy it, but memory discussion on this level is a topic a bit out of my understanding.

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

like image 255
tmsimont Avatar asked Oct 04 '22 02:10

tmsimont


1 Answers

Looking at the source for copy, it works ultimately by reading and writing chunks of 8KB size -- so it won't end up allocating a whole lot of the server's memory. This is of course what one would expect to happen, as people have already commented.

However, multiple copies of big files running in parallel can easily kill the performance of the storage medium (especially if it's made of spinning rust), which in turn can lead to the copies taking ridiculous amounts of time to complete. This could easily turn out to be longer than the script's max execution time limit, so it would be a good idea to remove the limit before starting the copy.

like image 186
Jon Avatar answered Oct 07 '22 19:10

Jon