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 bytesKí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 byteHà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 socketBố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 bytes1 có thể được sử dụng để ngăn chặn điều nàyThis 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 bytes2 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 bytes3 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 bytes1. 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 bytes5quảng cáoTrong 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 bytes6 đặ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 bytes7 xóa một tệpIn the code example, we utilize all the three functions:
file_exists
,touch
, andunlink
.$r = touch[$filename];Hàm
$ php file_existence.php fruits.txt The file size is: 40 bytes6 đượ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 bytes9if [!$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 bytes6 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
, andunlink
.$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 bytes7 xóa tệpIn 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 bytes0Đây là đầu ra của
In the code example, we utilize all the three functions:
file_exists
,touch
, andunlink
.$r = touch[$filename];3Hàm
In the code example, we utilize all the three functions:
file_exists
,touch
, andunlink
.$r = touch[$filename];4 sao chép tệp và hàmIn the code example, we utilize all the three functions:
file_exists
,touch
, andunlink
.$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 bytes1Trì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
, andunlink
.$r = touch[$filename];6In 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 bytes2Trì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 bytes3In the code example, we utilize all the three functions:
file_exists
,touch
, andunlink
.$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
, andunlink
.$r = touch[$filename];8 để xóaHàm
In the code example, we utilize all the three functions:
file_exists
,touch
, andunlink
.$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ếtIn 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 bytes4Chú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ạiIn 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 bytes5Dò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 bytes6Ở đâ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 bytes7Trong 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"; }1In 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 bytes8Đây là đầu ra của
if [!$r] { exit["Failed to touch $filename file\n"]; } else { echo "The file $filename has been created\n"; }2Hà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àmif [!$r] { exit["Failed to touch $filename file\n"]; } else { echo "The file $filename has been created\n"; }4 thay đổiIn 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 bytes9Đâ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 socket0Cầ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"; }5Hà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ẫuThe script determines the types of four files.
$ php file_types.php file dir char socket1Nế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 socket2Đâ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"; }7Hà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ệpThe script determines the types of four files.
$ php file_types.php file dir char socket3Đâ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 socket4Đầ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 socket5Tệp
$r = unlink[$filename];5 được tạo và xử lý tệp này được trả vềQuảng cáoThe script determines the types of four files.
$ php file_types.php file dir char socket6Chúng tôi đóng xử lý tệp bằng hàm
$r = unlink[$filename];6PHP đọ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 socket7In 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 bytes00 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 khiThe script determines the types of four files.
$ php file_types.php file dir char socket8Đâ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 bytes02quảng cáoTrong 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 bytes03, đọc một dòng từ phần xử lý tệpThe script determines the types of four files.
$ php file_types.php file dir char socket9Chú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 bytes04This script checks for the existence of a given file before it computes its size.
if [$argc != 2] { exit["Usage: file_existence.php filename\n"]; }0Với chức năng
$filename = $argv[1];8, chúng tôi mở một tay cầm đến một trang webThis script checks for the existence of a given file before it computes its size.
if [$argc != 2] { exit["Usage: file_existence.php filename\n"]; }1Chú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];7This 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 bytes08quảng cáoVí 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"]; }3Hà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 bytes09 loại bỏ các khoảng trắng ở đầu và cuốiQuảng cáoThis 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ệpquảng cáoChú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 socket5Hà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ạoQuảng cáoThis script checks for the existence of a given file before it computes its size.
if [$argc != 2] { exit["Usage: file_existence.php filename\n"]; }6Vớ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"]; }7Chú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 bytes15 cấp caoQuảng cáoThis script checks for the existence of a given file before it computes its size.
if [$argc != 2] { exit["Usage: file_existence.php filename\n"]; }8Tệ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 bytes16Thờ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 bytes17,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 bytes18 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 bytes19Quảng cáoThis 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 bytes20Quyề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 bytes21 nhận quyền đối với tệp;$filename = $argv[1];0Cá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 bytes22 chuyển biểu diễn thập phân sang bát phân$filename = $argv[1];1Trong 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 bytes23Quyề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 bytes24$filename = $argv[1];3Hà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 bytes24 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];4Cá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 bytes26 đọc một dòng từ tệp CSV [giá trị được phân tách bằng dấu phẩy]; . HàmIn 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 bytes27 định dạng một dòng dưới dạng CSV và ghi nó vào một tệp$filename = $argv[1];5Chú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 bytes28Hà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 bytes29 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àmIn 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 bytes29 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 PHPLà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ủ Đề