我可以讀/寫同一個文件嗎?如果是這樣,你能解釋我 怎麼做PHP文件的讀寫
Q
PHP文件的讀寫
1
A
回答
6
$file = "./test.txt";
// open file at the beginning.
$fh = fopen($file, 'r+');
//read the first line of the file. (advances pointer to the second line).
$contents = fread($fh);
// modify contents.
$new_contents = str_replace("hello world", "hello", $contents);
// make sure you're back at the 0 index.
fseek($file, 0);
// write
fwrite($fh, $new_contents);
// close.
fclose($fh);
// done!
-2
0
在PHP 5 file_put_contents
是最簡單的方法:
<?php
$file = 'people.txt';
$current = file_get_contents($file); // Open the file to get existing content
$current .= "John Smith\n"; // Append a new person to the file
file_put_contents($file, $current); // Write the contents back to the file
?>
+0
我仍然覺得有趣的是,file_get_contents的時間遠遠長於file_put_contents。 – cwallenpoole
-1
file_put_contents("write.txt",file_get_contents("read.txt"));
相關問題
- 1. 帶文件讀寫錯誤的PHP gzuncompress
- 2. 在php中讀寫dat文件
- 3. 在PHP中處理讀/寫文件?
- 4. 在PHP中讀寫tbz文件
- 5. 無法讀寫xlsx文件php
- 6. PHP:讀取和寫入Excel文件
- 7. 讀取和寫入遠程文件PHP
- 8. php文件讀取和寫入
- 9. 文件讀/寫VS數據庫讀/寫
- 10. 讀寫XML文件
- 11. 讀/寫「INI」文件
- 12. Java:CSV文件讀寫
- 13. 讀寫plist文件
- 14. Python文件(讀/寫)
- 15. 讀寫P7M文件
- 16. 讀寫文本文件
- 17. Python的TXT文件級讀寫文件
- 18. 讀取XML文件,並寫入一個PHP陣列/文件
- 19. 在寫入XML文件時讀取XML文件PHP
- 20. 閱讀XML文件並寫入到一個php數組/文件
- 21. 讀寫用PHP
- 22. 如何保護讀取/寫入交易與PHP文件在PHP
- 23. 文件的讀寫,而逐行讀取文件中的行
- 24. 閱讀php文件
- 25. PHP讀取文件
- 26. PHP - 寫在文件
- 27. PHP寫入文件
- 28. 寫入PHP文件?
- 29. php。寫入文件
- 30. 寫入.php文件?
懶得寫全面的樣本。 +1然後;-) – zerkms