如果您尝试像这样分块读取文件,是否会得到更好的结果?:
$my_file = fopen($temp_file, 'rb');
$encrypted_file_name = $target_file;
$encrypted_file = fopen($encrypted_file_name, 'wb');
stream_filter_append($encrypted_file, 'mcrypt.rijndael_128', STREAM_FILTER_WRITE, $opts);
//stream_copy_to_stream($my_file, $encrypted_file);
rewind($my_file);
while (!feof($my_file)) {
fwrite($encrypted_file, fread($my_file, 4096));
}
您也可以尝试致电stream_set_chunk_size http://php.net/stream_set_chunk_size打电话之前stream_copy_to_stream
设置复制到目标时用于从源流读取的缓冲区的大小。
希望有帮助。
编辑:我用这段代码进行了测试,当上传 700MB 的电影文件时,PHP 的内存使用峰值为 524,288 字节。看起来像stream_copy_to_stream
将尝试将整个源文件读入内存,除非您通过传递长度和偏移参数分块读取它。
$encrypted_file_name = $target_file;
$encrypted_file = fopen($encrypted_file_name, 'wb');
stream_filter_append($encrypted_file, 'mcrypt.rijndael_128', STREAM_FILTER_WRITE, $opts);
$size = 16777216; // buffer size of copy
$pos = 0; // initial file position
fseek($my_file, 0, SEEK_END);
$length = ftell($my_file); // get file size
while ($pos < $length) {
$writ = stream_copy_to_stream($my_file, $encrypted_file, $size, $pos);
$pos += $writ;
}
fclose($encrypted_file);
fclose($my_file);
unlink($temp_file);