텍스트 파일, 프로그램에 대한 사용자 파일이 더 있습니다. PHP에서 그룹 앞에 새 데이터를 삽입하려고합니다. 파일에 있습니다. 마지막 사용자가이 줄 위에 있고 마지막 사용자 및 위의 그룹 아래에 새 사용자를 삽입하고 싶습니다. 파일에파일에 텍스트 삽입 시도 특정 줄 위에
필자는 약간의 시도를하고 있었지만 그 줄 이후에만 사용할 수 있습니다.
은을 heres 내가
$key = 'groups:';
$newline = 'blackberry';
//copy file to prevent double entry
$file = "data2.yml";
$newfile = "filetemp.txt";
copy($file, $newfile) or exit("failed to copy $file");
//load file into $lines array
$fc = fopen ($file, "r");
while (!feof ($fc))
{
$buffer = fgets($fc, 4096);
$lines[] = $buffer;
}
fclose ($fc);
//open same file and use "w" to clear file
$f=fopen($newfile,"w") or die("couldn't open $file");
/* uncomment to debug */
print_r($lines);
print "
\n";
//loop through array using foreach
foreach($lines as $line)
{
fwrite($f,$line); //place $line back in file
if (strstr($line,$key)){ //look for $key in each line
fwrite($f,$newline."\n");
} //place $line back in file
}
fclose($f);
copy($newfile, $file) or exit("failed to copy $newfile");
?>
그것의 YML 파일이 무엇인지, 그래서 내가 업 후 또는 나사를 게시하는 여분의 줄을 추가하고 실행을 거부하지 못할.
감사합니다.
while (! feof() ...) 대신 file()을 사용하여 전체 파일을 배열 –