PHP上传大文件和处理大数据
1. 上传大文件
/* 以1.5M/秒的速度写入文件,防止一次过写入文件过大导致服务器出错(chy/20150327) */ $is_large_file = false; if( strlen($xml_str)>=2097152 ){ //当文件大于2M $is_large_file = true; fwrite($fp, $pre, strlen($pre)); //写入头部 $start = 0; while( $content=mb_strcut($xml_str,$start,1572864) ){ $start = $start + 1572864; $writeResult = fwrite($fp, $content, strlen($content)); if( !$writeResult ){ unlink($filename); break; } sleep(1); unset($content); } unset($xml_str); fwrite($fp, $end, strlen($end)); //写入尾部 }else{ $content = $pre.$xml_str.$end; fwrite($fp, $content, strlen($content)); }
2. 处理大数据的加密
//aes加密 public function aesEncode_large($info) { //.....(省略部分代码)
if(mcrypt_generic_init($cipher, $this->aesKey, $this->aesIv) != -1){ //$cipherText = mcrypt_generic($cipher, $beianInfo); //原普通的加密方式(chy/20150327) //处理大字符串加密。temp.text主要用于文件缓存(chy/20150327) $filename = B_ROOT."/admin/temp/temp.txt"; file_put_contents($filename,''); //将文件清空 $fp = fopen($filename, 'wb'); while( $content=mb_strcut($info,$start,104800) ){ $start = $start + 104800; $cipherTextCut = mcrypt_generic($cipher, $content); fwrite($fp, $cipherTextCut, strlen($cipherTextCut)); unset($cipherTextCut); unset($content); } mcrypt_generic_deinit($cipher); mcrypt_module_close($cipher); fclose($fp); return true; } else { return false; } }