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 tin trên máy chủ.
Tệp mở PHP - fopen()
Một phương pháp tốt hơn để mở tệp là sử dụng hàm fopen()
. Hàm này cung cấp cho bạn nhiều tùy chọn hơn hàm readfile()
.
Chúng ta 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 của fopen()
chứa tên của tệp sẽ được mở và tham số thứ hai chỉ định chế độ nào tệp sẽ được mở. 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:
Ví dụ
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
Chạy ví dụ » Mẹo: Hàm fread()
và fclose()
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 |
Tệp đọc PHP - fread()
Hàm fread()
đọc từ một file đang mở.
Tham số đầu tiên của fread()
chứa tên của tệp cần đọc và tham số thứ hai chỉ định số byte tối đa cần đọc.
Đoạn mã PHP sau đây đọc đến cuối tệp "webdictionary.txt":
fread($myfile,filesize("webdictionary.txt"));
Đóng tệp PHP - fclose()
Hàm fclose()
được sử dụng để đóng một file đang mở.
Đó là một cách lập trình tốt để đóng tất cả các tệp sau khi bạn xử lý xong chúng. Bạn không muốn một tệp đang mở chạy xung quanh máy chủ của mình chiếm nhiều tài nguyên!
fclose()
yêu cầu tên của tệp (hoặc một biến chứa 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ừ một tệp.
Ví dụ bên dưới xuất ra dòng đầu tiên của tệp "webdictionary.txt":
Ví dụ
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
Chạy ví dụ » Lưu ý: Sau khi gọi hàm fgets()
, con trỏ tệp đã di chuyển sang dòng tiếp theo.
Kiểm tra cuối tệp PHP - feof()
Hàm feof()
kiểm tra xem đã đạt đến "cuối tập tin" (EOF) 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ụ bên dưới đọc từng dòng tệp "webdictionary.txt" cho đến khi đạt đến cuối tệp:
Ví 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);
?>
Chạy ví dụ »PHP Đọc ký tự đơn - fgetc()
Hàm fgetc()
được sử dụng để đọc một ký tự từ một tệp.
Ví dụ bên dưới đọc từng ký tự trong tệp "webdictionary.txt" cho đến khi đạt đến cuối tệp:
Ví 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);
?>
Chạy ví dụ » Lưu ý: Sau khi gọi hàm fgetc()
, con trỏ tệp sẽ di chuyển đến ký tự tiếp theo.
Tham khảo hệ thống tập tin PHP hoàn chỉnh
Để 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 đầy đủ của chúng tôi.