From 92de8e02b8f792904fdb1fbac6978f7b43e07ff4 Mon Sep 17 00:00:00 2001 From: GIGABAIT93 Date: Wed, 15 Feb 2023 12:37:36 +0200 Subject: [PATCH] Update FileEditor.php --- src/FileEditor.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/FileEditor.php b/src/FileEditor.php index c7a53b3..a0dece8 100644 --- a/src/FileEditor.php +++ b/src/FileEditor.php @@ -36,13 +36,16 @@ public function closeFile() public function appendAfterWord($word, $text) { - $contents = file_get_contents($this->filename); - $position = strpos($contents, $word); - if ($position !== false) { - $start = substr($contents, 0, $position + strlen($word)); - $end = substr($contents, $position + strlen($word)); - file_put_contents($this->filename, $start . "\n" . $text . "\n" . $end); + if (file_exists($this->filename)) { + $contents = file_get_contents($this->filename); + $position = strpos($contents, $word); + if ($position !== false) { + $start = substr($contents, 0, $position + strlen($word)); + $end = substr($contents, $position + strlen($word)); + file_put_contents($this->filename, $start . "\n" . $text . "\n" . $end); + } } + } public function issetText($text) @@ -53,6 +56,7 @@ public function issetText($text) public static function appendAfter($file, $word, $text) { + if (file_exists($file)) { $contents = file_get_contents($file); $isset = strpos($contents, $text) !== false; if (!$isset) { @@ -63,5 +67,6 @@ public static function appendAfter($file, $word, $text) file_put_contents($file, $start . "\n" . $text . "\n" . $end); } } + } } -} \ No newline at end of file +}