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.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
