PHP uses the fopen function to create, open file details and instance code _php instance

Source: Internet
Author: User
Tags fread

There are no separate file creation functions in PHP, if we want to create a function, we can use fopen (), the fopen () function literally means to open the file, but the function also has the ability to create the file, and when you open a file using the fopen () function, you will try to create the file if it does not exist. and returns a resource.

Introduction to PHP fopen function

fopen function to open a file or URL

Grammar:

Resource fopen (string filename, string mode)

fopen () binds the name resource specified by filename to a stream.

Parameters:

1. FileName is the name of the file to try to open/create.

If filename is "scheme://..." Format, the Search protocol processor (also known as the Encapsulation Protocol) is treated as a url,php to handle this pattern. If the protocol has not yet registered the encapsulation protocol, PHP will issue a message to help check for potential problems in the script and continue with filename as a normal filename.

If PHP thinks that filename specifies a local file, it will attempt to open a stream on the file. The file must be accessible by PHP, so you need to confirm that the file access permission allows the access. If Safe mode is activated or open_basedir, further restrictions are applied.

If PHP believes that filename specifies a registered protocol and that the Protocol is registered as a network url,php will check and confirm that Allow_url_fopen has been activated. If it is turned off, PHP will issue a warning and the fopen call fails.

2. Mode specifies an open mode with the following possible values:

Open Mode Description
R Read-only and point the file pointer to the file start location
r+ Read and write, point the file pointer to the file start location
W Write only, point the file pointer to the beginning of the file, and empty the file, if the file does not exist, try to create the
w+ Read and write, point the file pointer at the beginning of the file and empty the contents of the file, if the file does not exist, try to create the
A Append, pointing the file pointer at the end of the file and attempting to create if the file does not exist
A + Read-write append, point the file pointer to the end of the file, and try to create the file if it does not exist
X Write only, and create the file, and if the file already exists, the fopen () call fails and returns FALSE
x+ Read and write and create the file, and if the file already exists, the fopen () call fails and returns FALSE

PHP fopen Function Instance

1. Create a file using the fopen function:

$my _file = ' file.txt ';//If the file does not exist (default is the current directory)
$handle = fopen ($my _file, ' W ') or Die (' Cannot open file: '. $my _file);//imp licitly creates file

2, use the fopen function to open the file:

$my _file = ' file.txt '//Suppose the file file.txt exists
$handle = fopen ($my _file, ' W ') or Die (' Cannot open file: '. $my _file);//open fi Le for writing (' W ', ' R ', ' a ') ...


3, fopen function combined with fread read files:

$my _file = ' file.txt ';
$handle = fopen ($my _file, ' R ');
$data = Fread ($handle, FileSize ($my _file));


4, the fopen function combines the fwrite function to write the file

$my _file = ' file.txt ';
$handle = fopen ($my _file, ' W ') or Die (' Cannot open file: '. $my _file);
$data = ' This is the ' data ';
Fwrite ($handle, $data);

5, the fopen function combines the Fwrite function to append content to the file:

$my _file = ' file.txt ';
$handle = fopen ($my _file, ' a ') or die (' Cannot open file: '. $my _file);
$data = ' New data line 1 ';
Fwrite ($handle, $data);
$new _data = "\ n". ' New data Line 2 ';
Fwrite ($handle, $new _data);

6. The fopen () function can also be used to open URL addresses on the Internet:

<?php
$fh = fopen ("http://www.baidu.com/", "R");
if ($FH) {while
  (!feof ($fh)) {
    echo fgets ($FH);
  }
? >

Note: fopen () returns only a resource, and you need to read and output the fgets () function to display the open page address.

Through this article hope to help everyone, thank you for your support to this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.