Chức năng hệ thống tệp PHP nào có thể được sử dụng để đóng tệp?

Các chức năng của hệ thống tệp được sử dụng để truy cập và thao tác với hệ thống tệp PHP cung cấp cho bạn tất cả các chức năng có thể cần để thao tác với một tệp

Cài đặt

Lỗi và chức năng ghi nhật ký là một phần của lõi PHP. Không cần cài đặt để sử dụng các chức năng này

Hàm Filesystem được sử dụng để truy cập và thao tác với hệ thống tập tin. Nó là một phần của mã PHP nên không cần cài đặt các chức năng này. Để truy cập các tệp trên hệ thống, đường dẫn tệp sẽ được sử dụng. Trên hệ thống Unix, dấu gạch chéo xuôi [/] được sử dụng làm dấu phân cách thư mục và trên nền tảng Windows, cả dấu gạch chéo xuôi [/] và dấu gạch chéo ngược [\] sẽ được sử dụng

cú pháp

file_type function[]

 

Thí dụ. Trong chương trình dưới đây, tệp có tên singleline. txt chứa một dòng thông tin duy nhất là “Tệp này bao gồm một dòng duy nhất. ”

Trong bài viết này, chúng tôi đề cập đến các hàm hệ thống tệp trong PHP. Chúng tôi làm việc với các tệp và thư mục, xác định quyền truy cập tệp và dung lượng ổ đĩa khả dụng cũng như đọc và ghi vào tệp. Đối với các ví dụ của chúng tôi, chúng tôi sử dụng PHP CLI. Hướng dẫn PHP là một hướng dẫn ngắn gọn về ngôn ngữ PHP trên ZetCode

$ php -v
php -v
PHP 8.1.2 [cli] [built: Aug  8 2022 07:28:23] [NTS]
...

Chúng tôi sử dụng phiên bản PHP 8. 1. 2

PHP có một bộ hàm phong phú để làm việc với các tệp và thư mục. PHP chứa cả hàm hệ thống tệp cấp thấp và cấp cao. Hàm

$filename = $argv[1];
8 là một ví dụ về hàm cấp thấp; . Hàm
$filename = $argv[1];
9 là một ví dụ về hàm hệ thống tệp PHP cấp cao

Hàm

$r = file_exists[$filename];

if [!$r] {

    exit["Cannot determine the size of the file; the file does not exist\n"];
}
0 trả về kích thước của tệp đã cho. Kích thước được chỉ định bằng byte

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes

Kích thước của tệp

$r = file_exists[$filename];

if [!$r] {

    exit["Cannot determine the size of the file; the file does not exist\n"];
}
1 là 40 byte

Hàm

$r = file_exists[$filename];

if [!$r] {

    exit["Cannot determine the size of the file; the file does not exist\n"];
}
2 nhận loại tệp. Các giá trị trả về có thể là.
$r = file_exists[$filename];

if [!$r] {

    exit["Cannot determine the size of the file; the file does not exist\n"];
}
3,
$r = file_exists[$filename];

if [!$r] {

    exit["Cannot determine the size of the file; the file does not exist\n"];
}
4,
$r = file_exists[$filename];

if [!$r] {

    exit["Cannot determine the size of the file; the file does not exist\n"];
}
5,
$r = file_exists[$filename];

if [!$r] {

    exit["Cannot determine the size of the file; the file does not exist\n"];
}
6,
$r = file_exists[$filename];

if [!$r] {

    exit["Cannot determine the size of the file; the file does not exist\n"];
}
7,
$filename = $argv[1];
9,

The script determines the types of four files.

$ php file_types.php
file
dir
char
socket

Bốn tệp là tệp thông thường, thư mục, thiết bị ký tự và ổ cắm

PHP kiểm tra sự tồn tại của tệp

Có thể xảy ra trường hợp chúng tôi muốn làm việc với một tệp không tồn tại. Hàm

$ php file_existence.php fruits.txt
The file size is: 40 bytes
1 có thể được sử dụng để ngăn chặn điều này

This script checks for the existence of a given file before it computes its size.

if [$argc != 2] {

    exit["Usage: file_existence.php filename\n"];
}

$ php file_existence.php fruits.txt
The file size is: 40 bytes
2 là một biến đặc biệt chứa số lượng đối số được truyền cho tập lệnh. Chúng tôi mong đợi hai đối số. tên tập lệnh và tên tệp khác được truyền dưới dạng tham số

$filename = $argv[1];

$ php file_existence.php fruits.txt
The file size is: 40 bytes
3 là một mảng các đối số được chuyển đến tập lệnh. Chúng tôi nhận được phần tử thứ hai

$r = file_exists[$filename];

if [!$r] {

    exit["Cannot determine the size of the file; the file does not exist\n"];
}

Ta kiểm tra xem file có tồn tại hay không bằng hàm

$ php file_existence.php fruits.txt
The file size is: 40 bytes
1. Nếu nó không tồn tại, chúng tôi chấm dứt tập lệnh bằng một tin nhắn

$ php file_existence.php fruits.txt
The file size is: 40 bytes

Đây là đầu ra mẫu của tập lệnh

$ php file_existence.php fruits.txt
The file size is: 40 bytes
5

quảng cáo

Trong ví dụ sau, chúng tôi tạo một tệp mới, xóa nó và kiểm tra sự tồn tại của nó. Hàm

$ php file_existence.php fruits.txt
The file size is: 40 bytes
6 đặt thời gian truy cập và sửa đổi tệp. Nếu tệp không tồn tại, nó được tạo. Hàm
$ php file_existence.php fruits.txt
The file size is: 40 bytes
7 xóa một tệp

In the code example, we utilize all the three functions: file_exists, touch, and unlink.

$r = touch[$filename];

Hàm

$ php file_existence.php fruits.txt
The file size is: 40 bytes
6 được sử dụng để tạo một tệp mới có tên là
$ php file_existence.php fruits.txt
The file size is: 40 bytes
9

if [!$r] {

    exit["Failed to touch $filename file\n"];
} else {

    echo "The file $filename has been created\n";
}

Một thông báo lỗi được in nếu chức năng

$ php file_existence.php fruits.txt
The file size is: 40 bytes
6 không thành công. Nhiều hàm PHP trả về giá trị

In the code example, we utilize all the three functions: file_exists, touch, and unlink.

$r = touch[$filename];
1 khi chúng bị lỗi

$r = unlink[$filename];

Hàm

$ php file_existence.php fruits.txt
The file size is: 40 bytes
7 xóa tệp

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
0

Đây là đầu ra của

In the code example, we utilize all the three functions: file_exists, touch, and unlink.

$r = touch[$filename];
3

Hàm

In the code example, we utilize all the three functions: file_exists, touch, and unlink.

$r = touch[$filename];
4 sao chép tệp và hàm

In the code example, we utilize all the three functions: file_exists, touch, and unlink.

$r = touch[$filename];
5 đổi tên tệp. Nếu tệp đích đã tồn tại, nó sẽ bị ghi đè

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
1

Trình xử lý lỗi tùy chỉnh được đặt với hàm

In the code example, we utilize all the three functions: file_exists, touch, and unlink.

$r = touch[$filename];
6

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
2

Trình xử lý nhận được một số lỗi và một chuỗi lỗi làm tham số

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
3

In the code example, we utilize all the three functions: file_exists, touch, and unlink.

$r = touch[$filename];
7 đưa ra kết quả này khi không có

In the code example, we utilize all the three functions: file_exists, touch, and unlink.

$r = touch[$filename];
8 để xóa

Hàm

In the code example, we utilize all the three functions: file_exists, touch, and unlink.

$r = touch[$filename];
9 trả về đường dẫn của thư mục mẹ. Kể từ PHP 7, chúng tôi có thể cung cấp một tham số cấp độ tùy chọn cho biết

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
4

Chúng tôi sử dụng chức năng

if [!$r] {

    exit["Failed to touch $filename file\n"];
} else {

    echo "The file $filename has been created\n";
}
0 để lấy thư mục chính của người dùng hiện tại

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
5

Dòng này in thư mục mẹ của thư mục chính của người dùng

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
6

Ở đây, chúng tôi in thư mục mẹ của thư mục làm việc hiện tại

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
7

Trong dòng này, chúng tôi in thư mục mẹ thứ hai của thư mục

if [!$r] {

    exit["Failed to touch $filename file\n"];
} else {

    echo "The file $filename has been created\n";
}
1

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
8

Đây là đầu ra của

if [!$r] {

    exit["Failed to touch $filename file\n"];
} else {

    echo "The file $filename has been created\n";
}
2

Hàm

if [!$r] {

    exit["Failed to touch $filename file\n"];
} else {

    echo "The file $filename has been created\n";
}
3 trả về thư mục làm việc hiện tại và hàm
if [!$r] {

    exit["Failed to touch $filename file\n"];
} else {

    echo "The file $filename has been created\n";
}
4 thay đổi

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
9

Đây là một đầu ra mẫu của kịch bản

Thư mục danh sách PHP

Trong năm ví dụ sau đây, chúng tôi liệt kê nội dung của các thư mục. Có một số cách tiếp cận để thực hiện nhiệm vụ này

The script determines the types of four files.

$ php file_types.php
file
dir
char
socket
0

Cần cung cấp đường dẫn đầy đủ của thư mục cho hàm

if [!$r] {

    exit["Failed to touch $filename file\n"];
} else {

    echo "The file $filename has been created\n";
}
5

Hàm

if [!$r] {

    exit["Failed to touch $filename file\n"];
} else {

    echo "The file $filename has been created\n";
}
6 tìm tên đường dẫn khớp với một mẫu

The script determines the types of four files.

$ php file_types.php
file
dir
char
socket
1

Nếu chúng ta chỉ định tham số thứ hai, tên hậu tố, nó cũng sẽ bị xóa khỏi tên đường dẫn

The script determines the types of four files.

$ php file_types.php
file
dir
char
socket
2

Đây là kết quả của ví dụ

if [!$r] {

    exit["Failed to touch $filename file\n"];
} else {

    echo "The file $filename has been created\n";
}
7

Hàm

if [!$r] {

    exit["Failed to touch $filename file\n"];
} else {

    echo "The file $filename has been created\n";
}
8 trả về thông tin về đường dẫn tệp

The script determines the types of four files.

$ php file_types.php
file
dir
char
socket
3

Đây là đầu ra

PHP tạo tập tin

Hàm

$filename = $argv[1];
8 mở một tệp hoặc một URL. Tham số đầu tiên của hàm là tên tệp và . Chẳng hạn, chế độ
$r = unlink[$filename];
0 chỉ mở để đọc và . Nếu chúng tôi mở một tệp ở chế độ
$r = unlink[$filename];
1 và nó không tồn tại, nó sẽ được tạo. Danh sách các chế độ có thể được tìm thấy tại hướng dẫn sử dụng PHP cho fopen[]

$filename = $argv[1];
8 trả về một điều khiển cho tệp. Đây là một đối tượng được sử dụng để thao tác với tệp;

The script determines the types of four files.

$ php file_types.php
file
dir
char
socket
4

Đầu tiên, chúng tôi kiểm tra xem tệp có tồn tại không

The script determines the types of four files.

$ php file_types.php
file
dir
char
socket
5

Tệp

$r = unlink[$filename];
5 được tạo và xử lý tệp này được trả về

Quảng cáo

The script determines the types of four files.

$ php file_types.php
file
dir
char
socket
6

Chúng tôi đóng xử lý tệp bằng hàm

$r = unlink[$filename];
6

PHP đọc tệp

Trong các ví dụ tiếp theo, chúng ta sẽ đọc nội dung tệp

$r = unlink[$filename];
7 đọc tối đa
$r = unlink[$filename];
8 byte từ con trỏ tệp được tham chiếu bởi tay cầm. Quá trình đọc dừng ngay sau khi đọc xong _______34_______8 byte hoặc đạt đến EOF [cuối tệp]

The script determines the types of four files.

$ php file_types.php
file
dir
char
socket
7

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
00 kiểm tra phần cuối của tệp trên một con trỏ tệp.
$r = unlink[$filename];
7 đọc tệp trên mỗi đoạn 1 KB cho đến khi

The script determines the types of four files.

$ php file_types.php
file
dir
char
socket
8

Đây là kết quả của ví dụ

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
02

quảng cáo

Trong ví dụ thứ hai, chúng tôi sử dụng hàm

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
03, đọc một dòng từ phần xử lý tệp

The script determines the types of four files.

$ php file_types.php
file
dir
char
socket
9

Chúng tôi sẽ phân tích tệp

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
04

This script checks for the existence of a given file before it computes its size.

if [$argc != 2] {

    exit["Usage: file_existence.php filename\n"];
}
0

Với chức năng

$filename = $argv[1];
8, chúng tôi mở một tay cầm đến một trang web

This script checks for the existence of a given file before it computes its size.

if [$argc != 2] {

    exit["Usage: file_existence.php filename\n"];
}
1

Chúng tôi đọc trang web cho đến khi kết thúc; . Trang được đọc trong khối 1 KB với chức năng

$r = unlink[$filename];
7

This script checks for the existence of a given file before it computes its size.

if [$argc != 2] {

    exit["Usage: file_existence.php filename\n"];
}
2

Đây là đầu ra của tập lệnh

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
08

quảng cáo

Ví dụ sau sử dụng chức năng cấp cao để đọc cùng một trang web

This script checks for the existence of a given file before it computes its size.

if [$argc != 2] {

    exit["Usage: file_existence.php filename\n"];
}
3

Hàm

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
09 loại bỏ các khoảng trắng ở đầu và cuối

Quảng cáo

This script checks for the existence of a given file before it computes its size.

if [$argc != 2] {

    exit["Usage: file_existence.php filename\n"];
}
4

Đây là đầu ra của ví dụ;

Hàm

$r = unlink[$filename];
4 ghi một chuỗi vào một tệp được tham chiếu bởi một bộ điều khiển tệp

quảng cáo

Chúng tôi mở một

$r = unlink[$filename];
5 ở chế độ ghi và viết bốn dòng vào đó

The script determines the types of four files.

$ php file_types.php
file
dir
char
socket
5

Hàm

$filename = $argv[1];
8 mở tệp ở chế độ ghi. Nếu tệp không tồn tại, nó sẽ tự động được tạo

Quảng cáo

This script checks for the existence of a given file before it computes its size.

if [$argc != 2] {

    exit["Usage: file_existence.php filename\n"];
}
6

Với chức năng

$r = unlink[$filename];
4, chúng tôi viết một dòng vào tệp. Hàm lấy phần xử lý tệp làm tham số đầu tiên của nó

This script checks for the existence of a given file before it computes its size.

if [$argc != 2] {

    exit["Usage: file_existence.php filename\n"];
}
7

Chúng tôi ghi vào tệp

$r = unlink[$filename];
5 và kiểm tra nội dung của nó

Chúng ta có thể ghi một chuỗi vào một tệp trong một lần bằng phương pháp

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
15 cấp cao

Quảng cáo

This script checks for the existence of a given file before it computes its size.

if [$argc != 2] {

    exit["Usage: file_existence.php filename\n"];
}
8

Tệp có thể được đọc và ghi nhưng không được thực thi đối với người dùng

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
16

Thời gian tệp PHP

Có ba loại thời gian tệp trên Linux. thời gian truy cập cuối cùng, thời gian thay đổi cuối cùng và thời gian sửa đổi cuối cùng. Các hàm PHP sau xác định những thời điểm này.

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
17,

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
18 và

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
19

Quảng cáo

This script checks for the existence of a given file before it computes its size.

if [$argc != 2] {

    exit["Usage: file_existence.php filename\n"];
}
9

Đây là đầu ra mẫu của tập lệnh

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
20

Quyền đối với tệp PHP

Hệ thống tệp thực thi quyền đối với tệp cho những người dùng và nhóm người dùng khác nhau. Hàm

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
21 nhận quyền đối với tệp;

$filename = $argv[1];
0

Các quyền trên Unix theo truyền thống được viết bằng biểu diễn bát phân. Hàm

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
22 chuyển biểu diễn thập phân sang bát phân

$filename = $argv[1];
1

Trong dòng này, chúng tôi kiểm tra xem quyền của tệp có cho phép chủ sở hữu của tệp đọc được không

$filename = $argv[1];
2

Đây là đầu ra mẫu của tập lệnh

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
23

Quyền truy cập tệp có thể được thay đổi bằng chức năng

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
24

$filename = $argv[1];
3

Hàm

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
24 chấp nhận quyền dưới dạng giá trị bát phân trong tham số thứ hai của nó. Giá trị bát phân được bắt đầu bằng 0

$filename = $argv[1];
4

Các quyền của tệp đã được thay đổi từ 664 thành 660

Hàm

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
26 đọc một dòng từ tệp CSV [giá trị được phân tách bằng dấu phẩy]; . Hàm

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
27 định dạng một dòng dưới dạng CSV và ghi nó vào một tệp

$filename = $argv[1];
5

Chúng tôi chạy tập lệnh và kiểm tra nội dung tệp

Trong ví dụ sau, chúng tôi đọc dữ liệu từ tệp CSV

$filename = $argv[1];
6

Đây là đầu ra của tập lệnh

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
28

Hàm

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
29 trả về tổng kích thước của hệ thống tệp hoặc phân vùng đĩa theo byte và hàm

In the example, we determine the size of the fruits.txt file. The file is located in the current working directory, that is, in the same directory as the PHP script.

$ php get_filesize.php
The file size is: 40 bytes
29 trả về

Đó là cách chính xác để đóng tệp trong PHP?

Hàm fclose[] đóng con trỏ tệp đang mở. Ghi chú. Tệp phải được mở bằng fopen[] hoặc fsockopen[].

Các chức năng của hệ thống tệp trong PHP là gì?

Hàm hệ thống tập tin PHP

Làm cách nào để mở và đóng tệp trong PHP?

Để tạo một tệp mới hoặc để mở một tệp hiện có, chúng tôi sử dụng hàm fopen[] do PHP cung cấp và để đóng tài nguyên tệp, hàm fclose[] được sử dụng .

Làm cách nào để sử dụng hệ thống tệp trong PHP?

Hệ thống tệp PHP cho phép chúng ta tạo tệp, đọc tệp theo từng dòng, đọc tệp theo từng ký tự, ghi tệp, nối tệp, xóa tệp và đóng tệp. .

Chủ Đề