❮ PHP Filesystem Reference
Example
Read one line from the open file:
<?php
$file = fopen("test.txt","r");
echo fgets($file);
fclose($file);
?>
Run Example »
Definition and Usage
The fgets() function returns a line from an open file.
Syntax
Parameter Values
file | Required. Specifies the open file to return a line from |
length | Optional. Specifies the number of bytes to read. Reading stops when length-1 bytes have been reached, or when a new line occurs, or on EOF. If no length is specified, it reads until end of the line |
Technical Details
A single line read from the file on success, FALSE on EOF or error |
4.0+ |
Yes, in PHP 4.3 |
More Examples
Example
Read open file, line by line:
<?php
$file = fopen("test.txt","r");
while(! feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>
Run Example »
❮ PHP Filesystem Reference
Learn PHP
PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's ASP.
Start learning PHP now »
Easy Learning with "PHP Tryit"
With our online "PHP Tryit" editor, you can edit the PHP code, and click on a button to view the result.
Example
<!DOCTYPE html>
<html>
<body>
<?php
echo "My first PHP script!";
?>
</body>
</html>
Try it Yourself »
Click on the "Try it Yourself" button to see how it works.
PHP Exercises
PHP Examples
Learn by examples! This tutorial supplements all explanations with clarifying examples.
See All PHP Examples
PHP Quiz Test
Learn by taking a quiz! This quiz will give you a signal of how much you know, or do not know, about PHP.
Start PHP Quiz!
My Learning
Track your progress with the free "My Learning" program here at W3Schools.
Log into your account, and start earning points!
This is an optional feature, you can study W3Schools without using My Learning.
PHP References
W3Schools' PHP reference contains different categories of all PHP functions, keywords and constants, along with examples.
Kickstart your career
Get certified by completing the course
Get certified
w3schoolsCERTIFIED.2022❮ Tham chiếu hệ thống tệp PHP
Thí dụ
Đọc một dòng từ tệp đang mở:
<?php
$file = fopen("test.txt","r");
echo fgets($file);
fclose($file);
?>
Định nghĩa và Cách sử dụng
Hàm fgets () trả về một dòng từ một tệp đang mở.
Cú pháp
Giá trị tham số
file | Required. Specifies the open file to return a line from |
length | Optional. Specifies the number of bytes to read. Reading stops when length-1 bytes have been reached, or when a new line occurs, or on EOF. If no length is specified, it reads until end of the line |
Chi tiết kỹ thuật
Một dòng duy nhất được đọc từ tệp thành công, FALSE trên EOF hoặc lỗi |
4.0+ |
Có, trong PHP 4.3 |
Các ví dụ khác
Thí dụ
Đọc tệp đang mở, từng dòng:
<?php
$file = fopen("test.txt","r");
while(! feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>
❮ Tham chiếu hệ thống tệp PHP
- Hàm fgets() dùng để trả về một dòng dữ liệu trong một tập tin đang được mở.
- Lưu ý: Hàm fgets() xác định một dòng dựa vào một trong ba điều kiện sau (tùy cái nào đến trước):
- Điểm kết thúc của dòng.
- Chiều dài được chỉ định.
- Điểm kết thúc của tập tin.
- Cú pháp:
fgets(file, length)- Trong đó:
file | Bắt buộc | - Tập tin đang được mở |
length | Không bắt buộc | - Chỉ định số byte được trả về (mặc định là 1024). |
- Tôi có một tập tin test.txt như sau:
- Đoạn mã thứ nhất:
<?php $file = fopen("test.txt","r"); echo fgets($file); //hiển thị chuỗi "tai lieu huong dan hoc HTML va CSS" fclose($file); ?>- Đoạn mã thứ hai:
<?php $file = fopen("test.txt", "r"); echo fgets($file, 20); //Hiển thị chuỗi "tai lieu huong d" fclose($file); ?>- Đoạn mã thứ ba:
<?php $file = fopen("test.txt", "r"); while(!feof($file)){ echo fgets($file); echo "<br>"; } fclose($file); ?>- Màn hình sẽ hiển thị là:
tai lieu huong dan hoc HTML va CSS ngon ngu lap trinh PHP JavaScript and jQuery