誰能告訴方法修改/刪除使用PHP文件中使用PHP
文件中使用PHP
回答
一個文本文件的內容,通過使用fopen處理:
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote to file ($filename)";
fclose($handle);
}
寫「」到要刪除的文件的內容。
通過線使用要刪除的內容行:
$arr = file($fileName);
unset($arr[3]); // 3 is an arbitrary line
然後寫入文件內容。或者你指的是內存映射文件?
您可以使用串流尋求http://uk3.php.net/manual/en/streamwrapper.stream- seek.php並編寫http://uk3.php.net/manual/en/streamwrapper.stream-write.php如果你使用fopen。 – Metalshark 2010-07-12 18:45:07
本教程可能會爲您提供您所需(二進制文件 - 如此低級別的文件編輯)http://en.kioskea.net/faq/998-parsing-a-binary-file-in-php – Metalshark 2010-07-12 18:48:15
非常感謝...陣列功能運行良好。 – ayush 2010-07-12 18:58:37
使用file_put_contents
:
file_put_contents($filename, 'file_content');
如果要追加到文件而不是替換它的內容使用方法:(file_out_contents
是使用整個fopen
複雜的簡單的替代)
file_put_contents($filename, 'append_this', FILE_APPEND);
有很多方法可以讀取文件。大文件可以很快處理使用
$fd = fopen ("log.txt", "r"); // you can use w/a switches to write append text
while (!feof ($fd))
{
$buffer = fgets($fd, 4096);
$lines[] = $buffer;
}
fclose ($fd);
您還可以使用file_get_contents()來讀取文件。它實現同樣事物的簡短方法。
修改或附加文件,你可以使用
$filePointer = fopen("log.txt", "a");
fputs($filePointer, "Text HERE TO WRITE");
fclose($filePointer);
你還可以將文件加載到陣列,然後執行搜索操作刪除的磁盤陣列的特定元素。
$lines = file('FILE WITH COMPLETE PATH.')
; //單個SLASH應該在路徑中雙擊如C://PATH//TO//FILE.TXT 上面的代碼將加載$ lines數組中的文件。
- 1. 在php文件中使用php變量()
- 2. PHP在多個PHP文件中使用臨時文件?
- 3. 在另一個PHP文件中使用PHP文件
- 4. 使用PHP上傳文件到777文件夾使用PHP
- 5. 使用php讀取目錄和文件中的文件使用php
- 6. 使用php下載文件在框中使用php
- 7. 使用php從文件.txt中排序信息使用php
- 8. 使用php提取xml文件集中的內容使用php
- 9. 使用php將csv文件插入到mysql表中使用php
- 10. 使用PHP讀取「塊」中的文件使用PHP
- 11. 使用PHP文件連接Prolog文件
- 12. 通過文件上傳使用php導入excel文件到php myadmin使用php
- 13. 當使用Ajax調用php文件時,php文件中的JavaScript不起作用
- 14. Zip文件中添加和使用PHP
- 15. 如何從XPS文件中使用PHP
- 16. 使用cron在PHP中編寫文件
- 17. 在PHP中使用RESTful上傳文件
- 18. 使用PHP中的excel文件
- 19. 使用PHP循環到JSON文件中
- 20. 如何在javascript文件中使用php
- 21. 在XML文件中使用PHP的XmlWriter
- 22. 在JS文件中使用PHP
- 23. 在php類中使用配置文件
- 24. PHP使用目錄中的文件
- 25. 呼籲PHP文件中使用jQuery
- 26. 如何在php中使用md5文件?
- 27. 錯誤上傳文件中使用PHP
- 28. 在php中使用ffmpeg exe文件
- 29. 創建使用XML文件中的PHP
- 30. 難度使用SQL PHP文件中
可能重複的[使用php清除文本文件的內容](http://stackoverflow.com/questions/1073609/clearing-content-of-text-file-using-php) – Gordon 2010-07-12 18:36:16
*(tutorial)* [Practical PHP - 第8章:文件](http://www.tuxradar.com/practicalphp/8/0/0) – Gordon 2010-07-12 18:38:08