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

What is the usage of php fopen function

coldplay.xixi
coldplay.xixiOriginal
2021-02-25 17:02:583352browse

php Usage of fopen function: [fopen()] function opens a file or URL. If the opening fails, this function returns FALSE, and the syntax is [fopen(filename, mode, include_path, context)].

What is the usage of php fopen function

The operating environment of this tutorial: Windows 7 system, PHP version 5.6, DELL G3 computer. This method is suitable for all brands of computers.

php fopen function usage:

<strong>fopen()</strong> function opens a file or URL.

If the opening fails, this function returns FALSE.

Syntax:

fopen(filename,mode,include_path,context)

What is the usage of php fopen function

Possible values ​​for the mode parameter

What is the usage of php fopen function

Description

fopen() Binds the name resource specified by filename to a stream. If filename is of the form "scheme://...", it is treated as a URL and PHP will search for a protocol handler (also called a wrapper protocol) to handle the scheme. If the wrapper protocol has not been registered for the protocol, PHP will emit a message to help check for potential problems in the script and continue execution of filename as if it were a normal filename.

If PHP thinks that filename specifies a local file, it will try to open a stream on the file. The file must be accessible to PHP, so you need to confirm that the file access permissions allow this access. If safe mode or open_basedir is activated further restrictions apply.

If PHP believes that filename specifies a registered protocol, and the protocol is registered as a network URL, PHP will check and confirm that allow_url_fopen has been activated. If it is closed, PHP will issue a warning and the call to fopen will fail.

Support for context was added in PHP 5.0.0.

Tips and Notes

Note: Different operating system families have different line ending conventions. When writing to a text file and want to insert a new line, you need to use operating system-compliant line endings. Unix-based systems use \n as the line-ending character, Windows-based systems use \r\n as the line-ending character, and Macintosh-based systems use \r as the line-ending character. If files are written with incorrect line endings, other applications may behave strangely when opening the files.

Windows provides a text conversion tag ("t") that can transparently convert \n to \r\n. Alternatively, "b" can be used to force binary mode so that the data is not converted. To use these flags, use either "b" or "t" as the last character of the mode argument.

The default conversion mode depends on the SAPI and PHP version used, so for portability it is encouraged to always specify the appropriate tags. If you are working with plain text files and use \n as the line terminator in your script, but you also want the files to be readable by other applications such as Notepad, use "t" in mode. Use "b" in all other cases.

If you do not specify the "b" flag when operating binary files, you may encounter some strange problems, including corrupted image files and strange problems with \r\n characters.

Note: For portability reasons, it is strongly recommended to always use the "b" flag when opening a file with fopen().

Comments: Once again, for portability reasons, it is strongly recommended that you rewrite code that relies on "t" mode to use the correct line terminator and change it to "b" model.

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");
?>

Related video recommendations: PHP programming from entry to proficiency

The above is the detailed content of What is the usage of php fopen function. 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