ProcessWire icon indicating copy to clipboard operation
ProcessWire copied to clipboard

Use of file_get_contents consumes a lot of memory in WireUpload

Open mdcurtis opened this issue 11 years ago • 2 comments

I run a site where large audio files are uploaded and have hit the default PHP memory limit (128M on my host) when uploading a 36M file. I was able to increase the limit, but large memory usage makes me uncomfortable, particularly if the upload was occurring while the site was busy. I suspect that this is due to line 141 of WireUpload (AJAX uploader):

file_put_contents($tmpName, file_get_contents('php://input')); 

Perhaps a buffered approach would be more friendly on memory? eg, (untested)

$phpInput = fopen( 'php://input', 'rb' );
$output = fopen( $tmpName, 'wb' );
while( !feof( $phpInput ) ) {
  $buffer = fread( $phpInfo, BUFFER_SIZE );
  if( $buffer === FALSE ) break;
  fwrite( $output, $buffer );
}
fclose( $phpInput );
fclose( $output );

I'll look into this further and try to do some testing in the next few days.

mdcurtis avatar Aug 16 '15 22:08 mdcurtis

Thanks @mdcurtis, that seems to make sense. Please let me know if that solves the issue you are describing and I'll be happy to modify it to work as you suggest.

ryancramerdesign avatar Aug 17 '15 15:08 ryancramerdesign

I've now down some basic testing and profiling and have prepared a patch: https://gist.github.com/mdcurtis/dc0c97754f2539ffdfef (was too small to be bothered with a PR)

Some profiling with memory_get_peak_usage() suggests that the memory usage is now ~constant rather than peaking at base+filesize.

mdcurtis avatar Aug 22 '15 01:08 mdcurtis