I want it 메모리에 파일을 읽을 수 있지만, 완전히 로드중 행별로 없이.
내 너무 커서 메모리에 파일을 열 수 있는 메모리 오류가 난 항상 아웃하기로 시도하시겠습니까 승인하십시오 나가버렷.
파일 크기는 1gb.
객체 지향 인터페이스입니다 클래스 파일에 대한 스플프일로비치 사용할 수 있습니다 - , http://php.net/manual/en/splfileobject.fgets.php (PHP 5 > = 5.1.0)
<?php
$file = new SplFileObject("file.txt");
// Loop until we reach the end of the file.
while (!$file->eof()) {
// Echo one line from the file.
echo $file->fgets();
}
// Unset the file to call __destruct(), closing the file handle.
$file = null;
/**
* @return Generator
*/
$fileData = function() {
$file = fopen(__DIR__ . '/file.txt', 'r');
if (!$file)
die('file does not exist or cannot be opened');
while (($line = fgets($file)) !== false) {
yield $line;
}
fclose($file);
};
이를 사용하여 다음과 같습니다.
foreach ($fileData() as $line) {
// $line contains current line
}
개별 파일 안에 흐름선 포리치 () 이 방법으로 처리할 수 있습니다.
참고: 발전기 > 5.5 = PHP 합니다.
버퍼링 기법을 사용하여 파일을 읽을 수.
$filename = "test.txt";
$source_file = fopen( $filename, "r" ) or die("Couldn't open $filename");
while (!feof($source_file)) {
$buffer = fread($source_file, 4096); // use a buffer of 4KB
$buffer = str_replace($old,$new,$buffer);
///
}
foreach (new SplFileObject(__FILE__) as $line) {
echo $line;
}
내가 어떻게 관리할 수 있는 이 매우 큰 파일 (테스트됨 최대 100G). S # 39 보다 및 it& fgets () 기능으로 문자열들을 읽는다.
$block =1024*1024;//1MB or counld be any higher than HDD block_size*2
if ($fh = fopen("file.txt", "r")) {
$left='';
while (!feof($fh)) {// read the file
$temp = fread($fh, $block);
$fgetslines = explode("\n",$temp);
$fgetslines[0]=$left.$fgetslines[0];
if(!feof($fh) )$left = array_pop($lines);
foreach ($fgetslines as $k => $line) {
//do smth with $line
}
}
}
fclose($fh);
스플프일로비치 대용량 파일을 다루는 데 있어서 유용합니다.
function parse_file($filename)
{
try {
$file = new SplFileObject($filename);
} catch (LogicException $exception) {
die('SplFileObject : '.$exception->getMessage());
}
while ($file->valid()) {
$line = $file->fgets();
//do something with $line
}
//don't forget to free the file handle.
$file = null;
}
<?php
echo '<meta charset="utf-8">';
$k= 1;
$f= 1;
$fp = fopen("texttranslate.txt", "r");
while(!feof($fp)) {
$contents = '';
for($i=1;$i<=1500;$i++){
echo $k.' -- '. fgets($fp) .'<br>';$k++;
$contents .= fgets($fp);
}
echo '<hr>';
file_put_contents('Split/new_file_'.$f.'.txt', $contents);$f++;
}
?>
읽을 수 있는 기능을 어레이입니다 반품하십시오.
function read_file($filename = ''){
$buffer = array();
$source_file = fopen( $filename, "r" ) or die("Couldn't open $filename");
while (!feof($source_file)) {
$buffer[] = fread($source_file, 4096); // use a buffer of 4KB
}
return $buffer;
}