OK, what am I missing? I am trying to clear a file if it exceeds 50 lines.
This is what I have so far.
$file = 'idata.txt';
$lines = count file($file);
if ($lines > 50){
$fh = fopen( 'idata.txt', 'w' );
fclose($fh);
}
OK, what am I missing? I am trying to clear a file if it exceeds 50 lines.
This is what I have so far.
$file = 'idata.txt';
$lines = count file($file);
if ($lines > 50){
$fh = fopen( 'idata.txt', 'w' );
fclose($fh);
}
$file = 'idata.txt';
$lines = count(file($file));
if ($lines > 50){
$fh = fopen( 'idata.txt', 'w' );
fclose($fh);
}
如果文件真的很大,你最好循环:
$file="verylargefile.txt";
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle)){
$line = fgets($handle);
$linecount++;
if(linecount > 50)
{
break;
}
}
应该完成这项工作,而不是内存中的整个文件。
计数的语法错误。将此行替换count file($file);
为
count(file($file));
您的语法有错误,应该count(file($file));
是 不建议对较大的文件使用此方法,因为它将文件加载到内存中。因此,在大文件的情况下它不会有帮助。这是解决此问题的另一种方法:
$file="idata.txt";
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle)){
if($linecount > 50) {
//if the file is more than 50
fclosh($handle); //close the previous handle
// YOUR CODE
$handle = fopen( 'idata.txt', 'w' );
fclose($handle);
}
$linecount++;
}