Loading index.php +10 −0 Original line number Diff line number Diff line Loading @@ -101,5 +101,15 @@ print "<h2>Разница дней: </h2>"; print $func->diffDays(new DateTimeImmutable(), new DateTimeImmutable("2025-01-01")); ?> <h1>Function 8</h1> <?php try { $func->readLogFile(__DIR__ . "/public/text.txt"); } catch (Exception $e) { print $e->getMessage(); } ?> </body> </html> No newline at end of file src/Functions.php +26 −0 Original line number Diff line number Diff line Loading @@ -181,5 +181,31 @@ class Functions $dateInterval = date_diff($dateStart, $dateEnd); return (int)$dateInterval->format("%a") ; } /** * Напиши функцию, которая принимает путь до файла, * проверяет, что файл существует и выводит пользователю весь контент файла * (файл можешь создать любой) * @param string $filePath путь до файла * @return void * @throws Exception */ public function readLogFile(string $filePath): void { if (file_exists($filePath)) { $text = ""; $file = fopen($filePath, 'rb'); while(!feof($file)) { $line = fgets($file); $text .= $line; } fclose($file); print $text; } else { throw new RuntimeException("File not found: $filePath"); } } } Loading
index.php +10 −0 Original line number Diff line number Diff line Loading @@ -101,5 +101,15 @@ print "<h2>Разница дней: </h2>"; print $func->diffDays(new DateTimeImmutable(), new DateTimeImmutable("2025-01-01")); ?> <h1>Function 8</h1> <?php try { $func->readLogFile(__DIR__ . "/public/text.txt"); } catch (Exception $e) { print $e->getMessage(); } ?> </body> </html> No newline at end of file
src/Functions.php +26 −0 Original line number Diff line number Diff line Loading @@ -181,5 +181,31 @@ class Functions $dateInterval = date_diff($dateStart, $dateEnd); return (int)$dateInterval->format("%a") ; } /** * Напиши функцию, которая принимает путь до файла, * проверяет, что файл существует и выводит пользователю весь контент файла * (файл можешь создать любой) * @param string $filePath путь до файла * @return void * @throws Exception */ public function readLogFile(string $filePath): void { if (file_exists($filePath)) { $text = ""; $file = fopen($filePath, 'rb'); while(!feof($file)) { $line = fgets($file); $text .= $line; } fclose($file); print $text; } else { throw new RuntimeException("File not found: $filePath"); } } }