FileServer.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace app\common\model\base\models;
  3. /**
  4. * @title : 文件处理
  5. * @desc :
  6. * @Author : Rock
  7. * @Date : 2023-04-18 09:44:11
  8. */
  9. class FileServer
  10. {
  11. /**
  12. * @title: 追加单行文本至文件尾
  13. * @desc: 描述
  14. * @param {string} {filename} {} {文件名}
  15. * @param {string} {content} {} {追加的文本内容}
  16. * @return {*}
  17. * @author: Rock
  18. * @method: POST
  19. * @Date: 2023-04-18 09:58:40
  20. */
  21. static public function appendLine(string $filename, string $content='')
  22. {
  23. $fp = fopen($filename,'a');
  24. fwrite($fp,$content);
  25. fclose($fp);
  26. }
  27. /**
  28. * @title: 追加多行文本至文件末尾
  29. * @desc: 描述
  30. * @param {array} {filename} {} {文件名}
  31. * @param {array} {content} {} {待追加的文本数组}
  32. * @return {*}
  33. * @author: Rock
  34. * @method: POST
  35. * @Date: 2023-04-18 09:58:52
  36. */
  37. static public function appendAll(string $filename,array $content=[])
  38. {
  39. $fp = fopen($filename,'a');
  40. foreach($content as $item){
  41. fwrite($fp,$item);
  42. }
  43. fclose($fp);
  44. }
  45. /**
  46. * @title: 从文件头写入单行文本
  47. * @desc: 描述
  48. * @param {string} {filename} {} {文件名}
  49. * @param {string} {content} {} {文本内容}
  50. * @return {*}
  51. * @author: Rock
  52. * @method: POST
  53. * @Date: 2023-04-18 10:14:18
  54. */
  55. static public function writeLine(string $filename,string $content='')
  56. {
  57. $fp = fopen($filename,'w');
  58. fwrite($fp,$content);
  59. fclose($fp);
  60. }
  61. /**
  62. * @title: 从文件头写入多行文本
  63. * @desc: 描述
  64. * @param {string} {filename} {} {文件名}
  65. * @param {array} {content} {} {文本内容数组}
  66. * @return {*}
  67. * @author: Rock
  68. * @method: POST
  69. * @Date: 2023-04-18 10:15:06
  70. */
  71. static public function writeAll(string $filename,array $content=[])
  72. {
  73. $fp = fopen($filename,'w');
  74. foreach($content as $item){
  75. fwrite($fp,$item);
  76. }
  77. fclose($fp);
  78. }
  79. }