Home  >  Article  >  Backend Development  >  What is the usage of php fopen

What is the usage of php fopen

藏色散人
藏色散人Original
2020-08-20 10:25:063000browse

php fopen is used to open a file or URL. Its syntax is "fopen(filename, mode, include_path, context)". The parameter filename represents the file or URL to be opened, and the parameter mode represents the access type.

What is the usage of php fopen

Recommended: "PHP Video Tutorial"

PHP fopen() function

Definition and usage

fopen() function opens a file or URL.

If fopen() fails, it will return FALSE with an error message. You can hide error output by adding an '@' in front of the function name.

Syntax

fopen(filename,mode,include_path,context)

Parameters

filename required. Specifies the file or URL to open.

mode Required. Specifies the type of access you are requesting to this file/stream.

Possible values:

"r" (open in read-only mode, point the file pointer to the file header)

"r " (open in read-write mode, set the file pointer to the file header) Point to the file header)

"w" (Open in writing mode, clear the file content, and try to create the file if it does not exist)

"w " (Open in reading and writing mode, clear the file content , if the file does not exist, try to create it)

"a" (Open in writing mode, point the file pointer to the end of the file for writing, if the file does not exist, try to create it)

"a " (Open for reading and writing, save the file contents by writing to the end of the file)

"x" (Create a new file and open for writing, if the file already exists Returns FALSE and an error)

"x " (Creates a new file and opens it for reading and writing, if the file already exists, returns FALSE and an error)

include_path Optional. Set this parameter to '1' if you also want to search for files in include_path (in php.ini).

context Optional. Specifies the environment for a file handle. context is a set of options that can modify the behavior of the stream.

Tips and Notes

Note: When writing a text file, make sure you use the correct line terminators! On Unix systems, the line terminator is \n; on Windows systems, the line terminator is \r\n; on Macintosh systems, the line terminator is \r. Windows systems provide a text conversion flag "t" that can transparently convert \n to \r\n. You can also use "b" to force binary mode so the data is not converted. To use these tags, use "b" or "t" as the last character of the mode argument.

Example

<?php
$file = fopen("test.txt","r");
$file = fopen("/home/test/test.txt","r");
$file = fopen("/home/test/test.gif","wb");
$file = fopen("http://www.example.com/","r");
$file = fopen("ftp://user:password@example.com/test.txt","w");
?>

The above is the detailed content of What is the usage of php fopen. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn