There was a task from php: // input to copy the data into the desired file.
I decided to check for speed possible ways to copy a 9 megabyte file from php: // stdin.
There are several:
- Read blocks in fread () and write in fwrite ()
- Writing content using file_put_contents () data obtained with file_get_contents ()
- Copy using copy () file copy function
- Using stream_copy_to_stream (). appeared in php5
Fread / fwrite results:Memory used: 9440 bytes.
Elapsed time: 0.09 seconds.
Results file_get_contents / file_put_contents:Memory consumption: 940 bytes.
Time spent: 0.06 seconds;
Copy () results:Memory used: 1008 bytes.
Time spent: 0.05 seconds;
')
Results stream_copy_to_stream:Memory used: 1240 bytes.
Time spent: 0.04 seconds;
What is the most interesting stream_copy_to_stream was faster copy (). If you take a regular file, NOT a stream, the results are likely to be in favor of copy ()
UPD: phpclub.ru/paste/index.php?show=1993 - test code
UPD 2: Tested on a file in 240MB. Everyone wins, except fread / fwrite (approximately 30 seconds versus 60).
So on large files it is not recommended to use it and file_get_contents due to the fact that you need the appropriate values of memory_limit in php.ini.
As a result, I made it on stream_copy_to_stream.