Hướng dẫn what is the use of fopen() function in php? - việc sử dụng hàm fopen() trong php là gì?

Cải thiện bài viết

Lưu bài viết

Hàm fopen () trong PHP là một hàm sẵn được sử dụng để mở tệp hoặc URL. Nó được sử dụng để liên kết một tài nguyên với hơi nước bằng cách sử dụng một tên tệp cụ thể. Tên tệp và chế độ cần kiểm tra được gửi dưới dạng tham số đến hàm fopen () và nó trả về tài nguyên con trỏ tệp nếu tìm thấy một trận đấu và sai khi lỗi. Đầu ra lỗi có thể được ẩn bằng cách thêm một ‘@@trước tên hàm.

Syntax:

resource fopen ( $file, $mode, $include_path, $context)

Các tham số được sử dụng: hàm fopen () trong PHP chấp nhận bốn tham số.
The fopen() function in PHP accepts four parameters.

  • $ File: Đây là một tham số bắt buộc chỉ định tệp. It is a mandatory parameter which specifies the file.
  • Mode $ $: Đây là một tham số bắt buộc chỉ định loại truy cập của tệp hoặc luồng. Nó có thể có các giá trị có thể sau: It is a mandatory parameter which specifies the access type of the file or stream.
    It can have the following possible values:
    • Cấm Riến: Nó chỉ đại diện cho đọc. Nó bắt đầu khi bắt đầu tập tin. It represents Read only. It starts at the beginning of the file.
    • Mùi R+: Nó đại diện cho đọc/ghi. Nó bắt đầu ở đầu tệp. It represents Read/Write.It starts at the beginning of the file.
    • Càng W W: Nó chỉ đại diện cho viết. Nó mở 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. It represents Write only.It opens and clears the contents of file or create a new file if it doesn’t exist.
    • Cấm W+: Nó đại diện cho đọc/viết. Nó mở 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. It represents Read/Write. It opens and clears the contents of file or creates a new file if it doesn’t exist.
    • Một người khác: Nó chỉ đại diện cho viết. 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. It represents Write only. It opens and writes to the end of the file or creates a new file if it doesn’t exist.
    • Một số người khác: nó đại diện cho đọc/viết. Nó bảo tồn nội dung tệp bằng cách ghi vào cuối tệp. It represents Read/Write. It preserves the file’s content by writing to the end of the file.
    • Cấm Xiêu: Nó chỉ đại diện cho viết. Nó tạo một tệp mới và trả về sai và lỗi nếu tệp đã tồn tại. It represents Write only. It creates a new file and returns FALSE and an error if the file already exists.
    • Cấm x+,: Nó đại diện cho Read/Write.it tạo một tệp mới và trả về sai và lỗi nếu tệp đã tồn tại. It represents Read/Write.It creates a new file and returns FALSE and an error if file already exists.
  • $ bao gồm_path: Đây là một tham số tùy chọn được đặt thành 1 nếu bạn muốn tìm kiếm tệp trong bao gồm_path (ví dụ php.ini). It is an optional parameter which is set to 1 if you want to search for the file in the include_path (Ex. php.ini).
  • $ Bối cảnh: Đây là một tham số tùy chọn được sử dụng để đặt hành vi của luồng. It is an optional parameter which is used to set the behavior of the stream.

Giá trị trả về: Nó trả về một tài nguyên con trỏ tệp thành công hoặc sai về lỗi.
It returns a file pointer resource on success, or FALSE on error.

Exceptions:

  • Khi ghi vào tệp văn bản, nên sử dụng ký tự kết thúc dòng chính xác dựa trên nền tảng. Ví dụ, các hệ thống UNIX sử dụng \ n, hệ thống Windows sử dụng các hệ thống Macintosh và Macintosh sử dụng \ r làm ký tự kết thúc dòng.
  • Nên sử dụng cờ ‘B, khi mở các tệp bằng fopen ().
  • Một lỗi của cấp độ e_warning được tạo nếu mở thất bại.
  • Khi chế độ an toàn được bật, PHP kiểm tra xem thư mục mà tập lệnh đang hoạt động có cùng UID (chủ sở hữu) như tập lệnh đang được thực thi hay không.
  • Nếu bạn không chắc chắn liệu tên tệp là tệp hoặc thư mục, bạn có thể cần sử dụng hàm is_dir () trước khi gọi fopen () vì hàm fopen () cũng có thể thành công khi tên tệp là một thư mục.

Dưới đây các chương trình minh họa hàm fopen ().

Chương trình 1:

$myfile = fopen(____1010____11__12

File does not exist!
4
File does not exist!
5
File does not exist!
6(____
File does not exist!
8
File does not exist!
9

portal for geeks!
0

Output:

File does not exist!

Chương trình 2:

portal for geeks!
1

$myfile = fopen(

portal for geeks!
6
File does not exist!
1
portal for geeks!
8
File does not exist!
3

portal for geeks!
6 =
portal for geeks!
8(____________
File does not exist!
9

geeksportalks
2
portal for geeks!
6
geeksportalks
4

geeksportalks
5$myfile
File does not exist!
9

portal for geeks!
0

Output:

portal for geeks!

Chương trình 3:

portal for geeks!
1

$myfile = fopen(

portal for geeks!
6
File does not exist!
16
File does not exist!
9

8 = $myfile0(____________

File does not exist!
1$myfile444 ____9________$myfile7

geeksportalks
5$myfile
File does not exist!
9

= 1 8

geeksportalks
4

portal for geeks!
0

Output:

portal for geeks!

Chương trình 4:

portal for geeks!
1

$myfile = fopen(

portal for geeks!
6
File does not exist!
1__82

fopen4$myfile

File does not exist!
1fopen7
File does not exist!
9

fopen9($myfile

File does not exist!
9

fopen4$myfile

File does not exist!
1(6
File does not exist!
9

fopen9(____________

File does not exist!
9

geeksportalks
2 $myfile0($myfile
File does not exist!
1$myfile44____9____
portal for geeks!
6$myfile7

geeksportalks
5$myfile
File does not exist!
9

portal for geeks!
0

Output:

geeksportalks

Reference:http://php.net/manual/en/function.fopen.php
http://php.net/manual/en/function.fopen.php


Việc sử dụng hàm fopen () trong PHP MCQ là gì?

17) Việc sử dụng hàm fopen () trong PHP là gì?Mô tả: Sprintf () là một hàm được xây dựng của PHP ghi một chuỗi được định dạng vào một biến.Nó trả về một chuỗi được định dạng.writes a formatted string to a variable. It returns a formatted string.

Làm thế nào tôi có thể sử dụng tệp fopen trong PHP?

$ myfile = fopen ("webDictionary.txt", "r") hoặc chết ("không thể mở tệp!");echo fread ($ myfile, fileSize ("webDictionary.txt"));fclose ($ myfile);... Thí dụ..

Tại sao Fopen được sử dụng?

Phương thức fopen () trong C là một hàm thư viện được sử dụng để mở một tệp để thực hiện các hoạt động khác nhau bao gồm đọc, viết vv.Along với các chế độ khác nhau.Nếu tệp tồn tại thì tệp cụ thể sẽ được mở một tệp mới được tạo.to open a file to perform various operations which include reading, writing etc. along with various modes. If the file exists then the particular file is opened else a new file is created.

Mục đích của fopen () và fclose () là gì?

Hàm fopen () được sử dụng để mở tệp hoặc url. Hàm fclose () đóng tệp đã mở. The fclose() function closes the opened file.