PHP fopen function



 The PHP fopen function can be used to open a file or URL content, such as any "openable through fopen" file under the root directory of the webpage or a file of an external resource, which can be applied to the opened file through different open modes (fopen mode) Above, PHP designers can easily modify the file or use the content of the file, but the use of PHP fopen often encounters the problem of server permission settings, and often encounters the conditional restrictions of different operating systems. You need to be familiar with the work of executing PHP fopen when using it. The environment is less error-prone.


PHP fopen function syntax rules
 resource fopen (string $filename, string $mode, bool $use_include_path = false, resource $context)
It can be seen from the grammar rules that fopen has four parameters in total, the most important is the first two, among which $filename is used to set the name or URL of the file to be opened, and the second parameter $mode is used Set the open mode, such as read-only (r), read-write (r+), write (w)..., etc. These modes are related to the degree to which the file opened by fopen can be operated. The third parameter $use_include_path and the fourth The parameter $context is a non-essential item and will be explained later.

When PHP fopen successfully opens a file or URL, it will automatically return the pointer resource, if it fails, it will return FALSE.

$filename -Specifies the file or URL to be opened. It must be the permission and content that PHP can open.

PHP fopen function common mode table ($mode)
mode
Description
'r'Open in reading mode, the pointer points to the beginning of the file.
'r+'Open in read-write mode, the pointer points to the beginning of the file.
'w'Open in writing mode, the pointer points to the beginning of the file and the file size is truncated to zero. Try to create the file if it does not exist.
'w+'Open in read-write mode, the pointer points to the beginning of the file and the file size is truncated to zero. Try to create the file if it does not exist.
'a'Open in writing mode, the pointer points to the end of the file, if the file does not exist, try to create it.
'a+'Open in read-write mode, the pointer points to the end of the file, if the file does not exist, try to create it.
'x'Create and open in write mode, if the file already exists, fopen returns false and generates an E_WARNING level error message
'x+'Create and open in read-write mode. If the file already exists, fopen returns false and generates an E_WARNING level error message
'c'Open the file in write mode, the pointer points to the beginning of the file, if the file does not exist, fopen tries to create it.
'c+'Open the file in read-write mode, the pointer points to the beginning of the file, if the file does not exist, fopen tries to create it.
$use_include_path -If it is necessary to search in the include_path, this parameter can be set to TRUE or 1, the default is FALSE, select the item.

$context -PHP 5.0.0 new features, support for context.

PHP fopen function example
<?php
$handle = fopen("/home/fopenbox/test.txt", "r");
$handle = fopen("/home/fopenbox/test.gif", "wb");
$handle = fopen( "http://www.php.net", "r");
$handle = fopen("ftp://user:password@example.com/test.txt", "w");
?>
Examples are the common ways to use the fopen function. The first line and the second line show the files that open the root directory of the webpage. The difference is in which mode is used to open. The third line is to open a webpage content. Fopen is not only possible Open the file, you can also open the content of a URL, which of course contains web content, but generally web pages can only be read by viewers, so our parameter is "r", and the fourth is to open FTP in write mode A file. The above is the common usage of fopen. Pay attention to whether the file has open read or write permissions, especially if it can accept fopen to open, there will be less error problems.

Post a Comment

0 Comments