我需要這方面的幫助,當我修改時有什么方法file1.txt可以將舊資訊發送到file2.txt等等。我有 4 個這樣的檔案,我想在file1.txt更改時交換其中的內容。
像這樣,當第一個內容發生變化時,發送鏈中的舊內容file1.txt--> file2.txt--> file3.txt-->file4.txt
那是我的 php 檔案
$myfile = fopen("file1.txt", "w") or die("Unable to open file!");
fwrite($myfile, "Today we had 11 people");
fclose($myfile);
我的想法是將此字串內容發送"Today we had 11 people"到第二個檔案,當內容file1.txt更改為讓我們說 "Today we had 23 people"
uj5u.com熱心網友回復:
簡單/實用的方法是復制鏈中的檔案。
function copyFiles(string $newLine): void
{
exec("cp path/to/file3.txt path/to/file4.txt");
exec("cp path/to/file2.txt path/to/file3.txt");
exec("cp path/to/file1.txt path/to/file2.txt");
exec("echo $newLine >> path/to/file1.txt");
}
注意:對于這種方法,您可能需要與您的主機交談,看看它是否被允許。不是每個主機都允許cp通過 php exec 命令。
來自@Will 的建議
您也可以為此使用 rename() 函式而不是 exec()。
rename("/test/file1.txt","/home/docs/my_file.txt");
uj5u.com熱心網友回復:
感謝@Maik Lowrey 的回答,cp我的主機不允許他指出。取而代之的是,我使用了@Will 的想法并完美地作業!謝謝你倆 :)
function copyFiles(string $newLine): void
{
rename( "file3.txt", "file4.txt");
rename("file2.txt", "file3.txt");
rename("file1.txt", "file2.txt");
exec("echo $newLine >> file1.txt");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/416992.html
標籤:
