Đọc file trong PHP
Code ví dụ:
php
1<!DOCTYPE html>2<html lang="vi">3 4<head>5 <meta charset="utf-8">6 <meta name="viewport" content="width=device-width, initial-scale=1.0">7 <meta name="keywords" content="Nền tảng,HTML,CSS,XML,JavaScript, Lập trình C#, Lập trình, Web, Kiến thức, Đồ án">8 <meta name="author" content="Dương Nguyễn Phú Cường">9 <meta name="description" content="Cung cấp các kiến thức Nền tảng, cơ bản về Lập trình, Lập trình web, Lập trình di động, Cơ sở dữ liệu, ...">10 <meta property="og:locale" content="vi_VN">11 <meta property="og:type" content="website">12 <meta property="og:title" content="Nền tảng Kiến thức">13 <meta property="og:description" content="Cung cấp các kiến thức Nền tảng, cơ bản về Lập trình, Lập trình web, Lập trình di động, Cơ sở dữ liệu, ...">14 <meta property="og:url" content="https://nentang.vn/">15 <meta property="og:site_name" content="Nền tảng Kiến thức">16 <title>Bài tập đọc file LOG | NenTang.vn</title>17</head>18 19<body>20 <h1>Đọc thông tin file LOG Hệ thống</h1>21 <?php22 ini_set('display_errors', 0);23 ini_set('display_startup_errors', 0);24 error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT);25 26 try {27 // Mở file để đọc (R - READ)28 $filePath = __DIR__ . '/log-2023-10-25.txt';29 $logfile = fopen($filePath, 'r');30 31 if($logfile) {32 // Đọc toàn bộ nội dung file (dùng cho file nhỏ)33 // echo fread($logfile, filesize($filePath));34 35 // Đọc từng dòng (dành cho file lớn)36 while( ($line = fgets($logfile)) !== false ) {37 echo $line . '<br />';38 }39 } else {40 echo 'Không tìm thấy file!';41 }42 43 // Đóng file44 fclose($logfile);45 }46 catch(DivisionByZeroError $ex) {47 echo $ex->getMessage();48 }49 catch(Exception $ex) {50 echo $ex->getMessage();51 }52 ?>53</body>54 55</html>Ghi file trong PHP
Code ví dụ:
php
1<!DOCTYPE html>2<html lang="vi">3 4<head>5 <meta charset="utf-8">6 <meta name="viewport" content="width=device-width, initial-scale=1.0">7 <meta name="keywords" content="Nền tảng,HTML,CSS,XML,JavaScript, Lập trình C#, Lập trình, Web, Kiến thức, Đồ án">8 <meta name="author" content="Dương Nguyễn Phú Cường">9 <meta name="description" content="Cung cấp các kiến thức Nền tảng, cơ bản về Lập trình, Lập trình web, Lập trình di động, Cơ sở dữ liệu, ...">10 <meta property="og:locale" content="vi_VN">11 <meta property="og:type" content="website">12 <meta property="og:title" content="Nền tảng Kiến thức">13 <meta property="og:description" content="Cung cấp các kiến thức Nền tảng, cơ bản về Lập trình, Lập trình web, Lập trình di động, Cơ sở dữ liệu, ...">14 <meta property="og:url" content="https://nentang.vn/">15 <meta property="og:site_name" content="Nền tảng Kiến thức">16 <title>Bài tập ghi file LOG | NenTang.vn</title>17</head>18 19<body>20 <h1>Ghi thông tin vào file LOG Hệ thống</h1>21 <form name="frmThaoTac" method="post" action="">22 <label for="">Thao tác</label>23 <select name="slThaoTac">24 <option value="read">Xem</option>25 <option value="create">Thêm</option>26 <option value="update">Sửa</option>27 <option value="delete">Xóa</option>28 </select>29 <br />30 31 <label for="">Ghi chú</label>32 <input type="text" name="txtGhiChu" />33 <br />34 35 <button name="btnLuu">Lưu log</button>36 </form>37 38 <?php39 if (isset($_POST['btnLuu'])) {40 // Thu thập thông tin từ người dùng gởi đến41 $thaotac = $_POST['slThaoTac'];42 $ghichu = $_POST['txtGhiChu'];43 44 $filePath = __DIR__ . '/log-2023-10-26.txt';45 if($logfile) {46 // Mở file để ghi đè (W - WRITE)47 $logfile = fopen($filePath, 'w') or die('Không thể mở file!');48 // Mở file để ghi vào cuối (A - APPEND)49 //$logfile = fopen($filePath, 'a') or die('Không thể mở file!');50 51 $txt = "Thao tác: $thaotac - Ghi chú: $ghichu";52 fwrite($logfile, $txt);53 }54 55 // Đóng file56 fclose($logfile);57 }58 ?>59</body>60 61</html>
