Hướng dẫn does php support file handling? - php có hỗ trợ xử lý tập tin không?


Xử lý tập tin là một phần quan trọng của bất kỳ ứng dụng web nào. Bạn thường cần mở và xử lý một tệp cho các tác vụ khác nhau.


Php thao tác các tập tin

PHP có một số chức năng để tạo, đọc, tải lên và chỉnh sửa các tệp.

Hãy cẩn thận khi thao tác các tập tin!

Khi bạn đang thao túng các tập tin, bạn phải rất cẩn thận.

Bạn có thể gây ra rất nhiều thiệt hại nếu bạn làm điều gì đó sai. Các lỗi phổ biến là: Chỉnh sửa tệp sai, điền vào ổ cứng bằng dữ liệu rác và xóa nội dung của một tệp một cách tình cờ.


Hàm readfile ()

Hàm readfile() đọc một tệp và ghi nó vào bộ đệm đầu ra.

Giả sử chúng tôi có một tệp văn bản có tên là "WebDictionary.txt", được lưu trữ trên máy chủ, trông như thế này:

Ajax = javaScript và xmlcss không đồng bộ = bảng kiểu cascading html = hyper text markup ngôn ngữ
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

Mã PHP để đọc tệp và ghi nó vào bộ đệm đầu ra như sau (hàm readfile() trả về số byte đọc trên thành công):

Hàm readfile() rất hữu ích nếu tất cả những gì bạn muốn làm là mở một tệp và đọc nội dung của nó.

Các chương tiếp theo sẽ dạy bạn nhiều hơn về xử lý tập tin.



Bài tập PHP



Trong thực tế, các tập lệnh PHP cần làm việc với dữ liệu được truy xuất từ ​​các tệp đĩa, bộ kết quả SQL, tài liệu XML và nhiều loại dữ liệu khác. PHP có nhiều chức năng tích hợp để truy cập các nguồn dữ liệu này. Hãy để hiểu về việc xử lý tệp trong PHP theo thứ tự sau:

  • Mở tập tin
  • Đóng tệp
  • Đọc tập tin
  • Viết tập tin
  • Các hoạt động xử lý tệp & thư mục khác

Xử lý tệp trong PHP: Mở tệp

Hướng dẫn does php support file handling? - php có hỗ trợ xử lý tập tin không?
Hàm fopen () được sử dụng để mở một tệp. Tham số đầu tiên chứa tên của tệp sẽ được mở và tham số thứ hai cho biết tệp sẽ được mở trong chế độ nào. Hàm fopen () tạo ra một con trỏ trỏ đến tệp được đề cập.

Example:

// a file is opened using fopen() function $file = fopen(“demo.txt”,'w'); ?>
// a file is opened using fopen() function
$file = fopen(“demo.txt”,'w');
?>

Các chế độ trong đó một tệp có thể được mở trong PHP là:

Chế độ Sự mô tả
r Chỉ đọc. Con trỏ tệp bắt đầu ở đầu tệp
r+ Đọc viết. Con trỏ tệp bắt đầu ở đầu tệp
w Chỉ viết. Nó mở ra và xóa nội dung của tệp; hoặc tạo một tệp mới nếu nó không tồn tại
w+ Đọc viết. Nó mở ra và xóa nội dung của tệp; hoặc tạo một tệp mới nếu nó không tồn tại
một Nối. Nó mở ra và ghi vào cuối tệp hoặc tạo một tệp mới nếu nó không tồn tại
a+ Đọc/nối. Nó bảo tồn nội dung tệp bằng cách ghi vào cuối tệp
x Chỉ viết. Tạo một tập tin mới. Trả về sai và lỗi nếu tệp đã tồn tại
x+ Đọc viết. Tạo một tập tin mới. Trả về sai và lỗi nếu tệp đã tồn tại

Xử lý tệp trong PHP: Đóng tệp

Hàm fclose () được sử dụng để đóng một tệp được trỏ bởi một con trỏ tệp mở. Nó lấy tệp làm đối số phải được đóng và đóng tệp đó.

NOTE:  

  • Một tệp phải được đóng đầu tiên bằng cách sử dụng hàm fclose () nếu nó đã được viết qua hàm fwrite () và bạn phải đọc nội dung của tệp.

  • fclose () không làm việc cho các tệp từ xa. Nó chỉ hoạt động trên các tệp có thể truy cập bởi hệ thống tệp máy chủ.

Example:

$file= fopen("demo.txt", "r"); // the file is closed using fclose() function readfile()0 ?>
$file= fopen("demo.txt", "r");
// the file is closed using fclose() function
readfile()0
?>

Di chuyển với xử lý tệp trong PHP, phần tiếp theo là đọc các tệp.

Đọc tập tin

API thao tác tệp PHP, cực kỳ linh hoạt: nó cho phép bạn đọc các tệp vào một chuỗi hoặc thành một mảng, từ hệ thống tệp cục bộ hoặc một URL từ xa, theo các dòng, byte hoặc ký tự.

  • fread (): hàm fread () được sử dụng để đọc nội dung của dữ liệu. Tham số đầu tiên là con trỏ tệp và cái còn lại là kích thước tệp tính bằng byte.: The fread() function is used to read the contents of the data. The first parameter is the file pointer and the other is the file size in bytes.

readfile()3 readfile()4 readfile()5 readfile()6 ?>
readfile()3
readfile()4
readfile()5
readfile()6
?>

  • Hàm Feof () có thể được sử dụng để kiểm tra xem có đạt được kết thúc của File (EOF) hay không. Điều này rất hữu ích vì chúng ta có thể lặp qua một tệp có độ dài không xác định. Chúng tôi có thể làm điều này trên mọi tệp trong bất kỳ chế độ nào. function can be used to check if the “End-Of-File” (EOF) has been reached. This is very useful as we can loop through a file of unknown length. We can do this on every file in any mode.

readfile()8 readfile()9 readfile()0 readfile()1 readfile()2 readfile()3 readfile()4 ?>
readfile()9
readfile()0
readfile()1
readfile()2
readfile()3
readfile()4
?>

  • fgetc (): hàm fgetc () được sử dụng để đọc một ký tự từ một tệp. The fgetc() function is used to read a character from a file.

readfile()6 readfile()7 readfile()0 readfile()1 0 readfile()3 readfile()4 ?>
readfile()7
readfile()0
readfile()1
0
readfile()3
readfile()4
?>

  • file_get_contents (): Để đọc các tệp đĩa cục bộ trong hàm file_get_contents () được sử dụng. Hàm này chấp nhận tên và đường dẫn của tệp đĩa và đọc toàn bộ tệp thành một biến chuỗi cùng một lúc. To read the local disk files in PHP, file_get_contents() function is used. This function accepts the name and path of a disk file, and reads the entire file into a string variable at once.

readfile()8 5 6 7 ?>
5
6
7
?>

  • File (): Một phương thức đọc dữ liệu thay thế từ tệp là hàm Php File (). Nó chấp nhận tên và đường dẫn của tệp và đọc toàn bộ tệp vào một mảng, có từng phần tử của mảng đại diện cho một dòng của tệp. Chúng tôi lặp lại thông qua mảng bằng cách sử dụng vòng lặp foreach để truy cập vào các phần tử của mảng. An alternative method of reading data from a file is PHP’s file() function. It accepts the name and path of the file and reads the entire file into an array, which has each element of the array representing one line of the file. We iterate through the array using foreach loop to access the elements of the array.

readfile()8 // a file is opened using fopen() function0 // a file is opened using fopen() function1 // a file is opened using fopen() function2 readfile()1 // a file is opened using fopen() function4 readfile()3 ?>
// a file is opened using fopen() function0
// a file is opened using fopen() function1
// a file is opened using fopen() function2
readfile()1
// a file is opened using fopen() function4
readfile()3
?>

  • Đọc các tệp từ xa: Cả File_Get_Contents () và File () hỗ trợ đọc dữ liệu từ URL, sử dụng giao thức HTTP hoặc FTP. Nó đọc một tệp HTML của web thành một mảng. Both file_get_contents() and file() support reading data from URLs, using either the HTTP or FTP protocol. It reads an HTML file of the Web into an array.

readfile()8 // a file is opened using fopen() function8 // a file is opened using fopen() function9 // a file is opened using fopen() function2 readfile()1 // a file is opened using fopen() function4 readfile()3 ?>
// a file is opened using fopen() function8
// a file is opened using fopen() function9
// a file is opened using fopen() function2
readfile()1
// a file is opened using fopen() function4
readfile()3
?>

Lưu ý: Trong trường hợp các liên kết mạng chậm, việc đọc một tệp từ xa trong các đoạn, đó là hiệu quả hơn, để tối đa hóa hiệu quả của băng thông mạng có sẵn. Hàm fgets () có thể đọc một số byte cụ thể từ một tệp.In case of slow network links, it is more efficient to read a remote file in “chunks,” to maximize the efficiency of available network bandwidth. fgets() function can read a specific number of bytes from a file.

readfile()8 $file = fopen(“demo.txt”,'w'); 6 $file = fopen(“demo.txt”,'w'); 7 $file = fopen(“demo.txt”,'w'); 8 readfile()0 readfile()1 ?>1 ?>2 ?>3 readfile()4 7 ?>6
$file = fopen(“demo.txt”,'w'); 6
$file = fopen(“demo.txt”,'w'); 7
$file = fopen(“demo.txt”,'w'); 8
readfile()0
readfile()1
?>1
?>2
?>3
readfile()4
7
?>

Viết tập tin

  • fwrite (): hàm & nbsp; fwrite () & nbsp; hàm được sử dụng để ghi vào một tệp. Tham số đầu tiên của & nbsp; fwrite () & nbsp; chứa tên của tệp để ghi và tham số thứ hai là chuỗi được viết.The fwrite() function is used to write to a file. The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written.

readfile()8 ?>8 ?>9 0 readfile()4 ?>
?>8
?>9
0
readfile()4
?>

  • Ghi đè PHP: Demo demo.txt có chứa một số dữ liệu. Tất cả các dữ liệu hiện có sẽ bị xóa và chúng tôi bắt đầu với một tệp trống. “demo.txt” contains some data. All the existing data will be ERASED and we start with an empty file.

readfile()8 ?>8 ?>9 0 readfile()4 ?>
?>8
?>9
0
readfile()4
?>

  • Ghi đè PHP: Demo demo.txt có chứa một số dữ liệu. Tất cả các dữ liệu hiện có sẽ bị xóa và chúng tôi bắt đầu với một tệp trống. The file_put_contents() function accepts a filename and the data to be written to the file.

file_put_contents (): hàm file_put_contents () chấp nhận tên tệp và dữ liệu sẽ được ghi vào tệp.
$file= fopen("demo.txt", "r"); 0
$file= fopen("demo.txt", "r"); 1
$file= fopen("demo.txt", "r"); 2
$file= fopen("demo.txt", "r"); 3
?>

readfile()8 $file= fopen("demo.txt", "r"); 0 $file= fopen("demo.txt", "r"); 1 $file= fopen("demo.txt", "r"); 2 $file= fopen("demo.txt", "r"); 3 ?>

  • Các hoạt động xử lý tệp và thư mục khác The filesize() function calculates the size of a file in bytes. It takes file name as an argument.

Tính toán kích thước tệp: Hàm fileSize () Tính toán kích thước của tệp tính bằng byte. Nó lấy tên tệp làm đối số.
$file= fopen("demo.txt", "r"); 6
$file= fopen("demo.txt", "r"); 7
$file= fopen("demo.txt", "r"); 8
$file= fopen("demo.txt", "r"); 9
// the file is closed using fclose() function 0
readfile()3
?>

  • readfile()8 $file= fopen("demo.txt", "r"); 6 $file= fopen("demo.txt", "r"); 7 $file= fopen("demo.txt", "r"); 8 $file= fopen("demo.txt", "r"); 9 // the file is closed using fclose() function 0 readfile()3 ?> The realpath() function is used to retrieve the absolute file path to a file.

Tìm đường dẫn tệp tuyệt đối: hàm realpath () được sử dụng để truy xuất đường dẫn tệp tuyệt đối đến một tệp.
// the file is closed using fclose() function 4
$file= fopen("demo.txt", "r"); 7
// the file is closed using fclose() function 6
$file= fopen("demo.txt", "r"); 9
// the file is closed using fclose() function 0
readfile()3
?>

readfile()8 // the file is closed using fclose() function 4 $file= fopen("demo.txt", "r"); 7 // the file is closed using fclose() function 6 $file= fopen("demo.txt", "r"); 9 // the file is closed using fclose() function 0 readfile()3 ?> We can also use the pathinfo() function. It returns an array containing the file’s path, name, and extension.

Lưu ý: Chúng tôi cũng có thể sử dụng hàm pathinfo (). Nó trả về một mảng chứa đường dẫn, tên và phần mở rộng của tệp.
readfile()02
$file= fopen("demo.txt", "r"); 7
readfile()04
$file= fopen("demo.txt", "r"); 9
// the file is closed using fclose() function 0
readfile()3
?>

  • readfile()8 readfile()02 $file= fopen("demo.txt", "r"); 7 readfile()04 $file= fopen("demo.txt", "r"); 9 // the file is closed using fclose() function 0 readfile()3 ?>We can get detailed information about a particular file, including its ownership, permissions, and modification and access times, using stat() function. It returns this information as an associative array.

Truy xuất các thuộc tính tệp: Chúng tôi có thể nhận được thông tin chi tiết về một tệp cụ thể, bao gồm quyền sở hữu, quyền và thời gian sửa đổi và truy cập của nó, sử dụng hàm stat (). Nó trả về thông tin này như một mảng kết hợp.
readfile()10
$file= fopen("demo.txt", "r"); 7
readfile()12
$file= fopen("demo.txt", "r"); 9
// the file is closed using fclose() function 0
readfile()3
?>

  • readfile()8 readfile()10 $file= fopen("demo.txt", "r"); 7 readfile()12 $file= fopen("demo.txt", "r"); 9 // the file is closed using fclose() function 0 readfile()3 ?> We can check if a file is readable, writable or executable with the help of is_readable(), is_writable(), and is_executable() functions.

Kiểm tra các chế độ: Chúng tôi có thể kiểm tra xem một tệp có thể đọc được, có thể ghi hoặc thực thi với sự trợ giúp của các hàm is_readable (), is_writable () và is_executable ().
readfile()10
readfile()19
$file= fopen("demo.txt", "r"); 7
readfile()21
readfile()22
readfile()23
readfile()24
readfile()3
readfile()26
readfile()27
readfile()28
readfile()3
readfile()30
readfile()31
readfile()32
readfile()3
$file= fopen("demo.txt", "r"); 9
// the file is closed using fclose() function 0
readfile()3
?>

  • Kiểm tra xem nó có phải là tệp hay không: hàm is_dir () trả về true nếu đối số được truyền cho nó là một thư mục. Hàm is_file () trả về true nếu đối số được truyền cho nó là một tệp.The is_dir() function returns true if the argument passed to it is a directory. The is_file() function returns true if the argument passed to it is a file.

readfile()8 readfile()39 $file= fopen("demo.txt", "r"); 7 readfile()41 readfile()42 readfile()3 readfile()44 readfile()45 readfile()3 ?>
readfile()39
$file= fopen("demo.txt", "r"); 7
readfile()41
readfile()42
readfile()3
readfile()44
readfile()45
readfile()3
?>

  • Sao chép tệp: Hàm Copy () sao chép tệp từ vị trí này sang vị trí khác. Nó có nguồn tệp và đường dẫn đích như hai đối số The copy() function copies a file from one location to another location. It has the file source and destination path as two arguments

readfile()8 readfile()49 $file= fopen("demo.txt", "r"); 7 readfile()51 readfile()52 $file= fopen("demo.txt", "r"); 9 readfile()54 readfile()3 $file= fopen("demo.txt", "r"); 9 readfile()45 readfile()3 ?>
readfile()49
$file= fopen("demo.txt", "r"); 7
readfile()51
readfile()52
$file= fopen("demo.txt", "r"); 9
readfile()54
readfile()3
$file= fopen("demo.txt", "r"); 9
readfile()45
readfile()3
?>

  • Đổi tên các tệp hoặc thư mục: hàm đổi tên () được sử dụng để đổi tên hoặc di chuyển một tệp (hoặc thư mục). Tôi lấy tên đường dẫn cũ và mới làm đối số. The rename() function is used to rename or movee a file (or directory). I takes the old and new path names as arguments.

readfile()8 readfile()61 $file= fopen("demo.txt", "r"); 7 readfile()63 readfile()64 $file= fopen("demo.txt", "r"); 9 readfile()66 ____23 $file= fopen("demo.txt", "r"); 9 readfile()45 readfile()3
readfile()61
$file= fopen("demo.txt", "r"); 7
readfile()63
readfile()64
$file= fopen("demo.txt", "r"); 9
readfile()66
readfile()3
$file= fopen("demo.txt", "r"); 9
readfile()45
readfile()3
readfile()71
readfile()72
readfile()73
readfile()74
$file= fopen("demo.txt", "r"); 9
readfile()76
readfile()3
$file= fopen("demo.txt", "r"); 9
readfile()79
readfile()3
?>

  • Xóa các tệp hoặc thư mục: hàm hủy liên kết () được sử dụng để xóa một tệp. Nó lấy tên tệp làm đối số. The unlink() function is used to remove a file. It takes the filename name as an argument.

readfile()8 readfile()83 $file= fopen("demo.txt", "r"); 7 readfile()85 readfile()86 $file= fopen("demo.txt", "r"); 9 readfile()88 readfile()3 $file= fopen("demo.txt", "r"); 9 readfile()45 readfile()3 ?>
readfile()83
$file= fopen("demo.txt", "r"); 7
readfile()85
readfile()86
$file= fopen("demo.txt", "r"); 9
readfile()88
readfile()3
$file= fopen("demo.txt", "r"); 9
readfile()45
readfile()3
?>

  • Khóa và mở khóa các tệp: Hàm Flock () khóa một tệp trước khi đọc hoặc ghi nó để không thể truy cập được bởi một quy trình khác. Thực hiện điều này làm giảm khả năng tham nhũng dữ liệu có thể xảy ra nếu hai quá trình cố gắng ghi dữ liệu khác nhau vào cùng một tệp ngay lập tức. Tham số đầu tiên lấy tên tệp. Tham số thứ hai cho Flock () Chỉ định loại khóa: Lock_ex tạo khóa độc quyền để viết, Lock_SH tạo khóa không độc quyền để đọc và Lock_un phá hủy khóa. The flock() function locks a file before reading or writing it so that it cannot be accessed by another process. Doing this reduces the possibility of data corruption that might occur if two processes attempt to write different data to the same file at the same instant. The first parameter takes the file name. The second parameter to flock() specifies the type of lock: LOCK_EX creates an exclusive lock for writing, LOCK_SH creates a non-exclusive lock for reading, and LOCK_UN destroys the lock.

readfile()8 readfile()95 $file= fopen("demo.txt", "r"); 0 readfile()97 readfile()98 readfile()99 readfile()00 readfile()01 readfile()02 readfile()4 $file= fopen("demo.txt", "r"); 3
readfile()95
$file= fopen("demo.txt", "r"); 0
readfile()97
readfile()98
readfile()99
readfile()00
readfile()01
readfile()02
readfile()4
$file= fopen("demo.txt", "r"); 3
?>

Đọc và viết tệp là các nhiệm vụ cơ bản mà bất kỳ nhà phát triển PHP nào cũng nên biết cách hoàn thành; Bài viết này bao gồm cả hai nhiệm vụ này, và nhiều hơn nữa. Nó bắt đầu với một minh chứng về cách đọc các tệp cục bộ và từ xa, cũng như cách tạo các tệp mới hoặc nối dữ liệu vào các tệp hiện có. Sau đó, nó chuyển sang thao tác thư mục theo PHP và giải thích nhiều chức năng thư mục và tệp PHP.

Với điều này, chúng tôi đã kết thúc việc xử lý tập tin này trong bài viết của PHP. Tôi hy vọng xử lý tập tin không nên là một vấn đề. Kiểm tra & nbsp; đào tạo chứng nhận PHP & nbsp; bởi Edureka, & nbsp;heck out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Có một câu hỏi cho chúng tôi? Vui lòng đề cập đến nó trong phần bình luận của xử lý tập tin trong Php và tôi sẽ liên hệ lại với bạn.

Việc xử lý tệp trong PHP là gì?

Xử lý tệp trong PHP: Mở tệp hàm fopen () được sử dụng để mở tệp.Tham số đầu tiên chứa tên của tệp sẽ được mở và tham số thứ hai cho biết tệp sẽ được mở trong chế độ nào.Hàm fopen () tạo ra một con trỏ trỏ đến tệp được đề cập.The fopen() function is used to open a file. The first parameter contains the name of the file to be opened and the second parameter tells in which mode the file should be opened. fopen() function creates a pointer which points to the file mentioned.

PHP có thể đọc từ tập tin không?

Php Read File - fread () hàm fread () đọc từ một tệp 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ố lượng byte tối đa để đọc.The fread() function reads from an open file. The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.

Có bao nhiêu chế độ xử lý tệp trong PHP?

Trong xử lý tệp PHP, có bốn bộ chế độ có thể.Đó là, {r và r+} - để đọc các tệp hiện có.four sets of possible modes. These are, {r and r+} – To read the existing files.

PHP có thể ghi vào tệp không?

PHP ghi vào tệp - fwrite () hàm fwrite () được sử dụng để ghi vào tệp.Tham số đầu tiên của fwrite () chứa tên của tệp để ghi vào và tham số thứ hai là chuỗi được viết.The fwrite() function is used to write to a file. The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written.