Mở / Đọc / Đóng tệp PHP
Trong chương này, chúng tôi sẽ hướng dẫn bạn cách mở, đọc và đóng một tệp trên máy chủ.
Tệp mở PHP - fopen ()
Một phương pháp tốt hơn để mở tệp là với fopen()
hàm. Chức năng này cung cấp cho bạn nhiều lựa chọn hơn readfile()
chức năng.
Chúng tôi sẽ sử dụng tệp văn bản, "webdictionary.txt", trong các bài học:
AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language
Tham số đầu tiên fopen()
chứa tên của tệp sẽ được mở và tham số thứ hai chỉ định tệp sẽ được mở ở chế độ nào. Ví dụ sau cũng tạo ra một thông báo nếu hàm fopen () không thể mở tệp được chỉ định:
Thí dụ
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
Mẹo: Các fread()
và các fclose()
chức năng sẽ được giải thích bên dưới.
Tệp có thể được mở ở một trong các chế độ sau:
Modes | Description |
---|---|
r | Open a file for read only. File pointer starts at the beginning of the file |
w | Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
a | Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x | Creates a new file for write only. Returns FALSE and an error if file already exists |
r+ | Open a file for read/write. File pointer starts at the beginning of the file |
w+ | Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
a+ | Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x+ | Creates a new file for read/write. Returns FALSE and an error if file already exists |
PHP đọc tệp - fread ()
Hàm fread()
đọc từ một tệp đang mở.
Tham số đầu tiên của fread()
chứa tên của tệp để đọc và tham số thứ hai chỉ định số byte tối đa để đọc.
Đoạn mã PHP sau đọc đến cuối tệp "webdictionary.txt":
fread($myfile,filesize("webdictionary.txt"));
PHP Đóng tệp - fclose ()
Hàm fclose()
được sử dụng để đóng một tệp đang mở.
Thực hành lập trình tốt là đóng tất cả các tệp sau khi bạn đã hoàn thành chúng. Bạn không muốn một tệp đang mở chạy xung quanh trên máy chủ của mình chiếm tài nguyên!
Yêu fclose()
cầu tên của tệp (hoặc một biến giữ tên tệp) mà chúng tôi muốn đóng:
<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>
PHP Đọc một dòng - fgets ()
Hàm fgets()
được sử dụng để đọc một dòng từ tệp.
Ví dụ dưới đây xuất ra dòng đầu tiên của tệp "webdictionary.txt":
Thí dụ
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
Lưu ý: Sau khi gọi fgets()
hàm, con trỏ tệp đã chuyển sang dòng tiếp theo.
Kiểm tra cuối tệp trong PHP - feof ()
Hàm feof()
kiểm tra xem "cuối tệp" (EOF) đã đạt đến hay chưa.
Hàm feof()
rất hữu ích để lặp qua dữ liệu có độ dài không xác định.
Ví dụ dưới đây đọc từng dòng tệp "webdictionary.txt", cho đến khi đến cuối tệp:
Thí dụ
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>
PHP Đọc một ký tự - fgetc ()
Hàm fgetc()
được sử dụng để đọc một ký tự từ tệp.
Ví dụ dưới đây đọc từng ký tự của tệp "webdictionary.txt" cho đến khi đạt đến cuối tệp:
Thí dụ
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?>
Lưu ý: Sau khi gọi fgetc()
hàm, con trỏ tệp sẽ di chuyển đến ký tự tiếp theo.
Toàn bộ tham chiếu hệ thống tệp PHP
Để có tài liệu tham khảo đầy đủ về các chức năng của hệ thống tệp, hãy truy cập Tài liệu tham khảo hệ thống tệp PHP hoàn chỉnh của chúng tôi .