Operation of PHP files

Source: Internet
Author: User
Tags flock fread readfile

This article introduces the contents of the PHP file operation, has a certain reference value, now share to everyone, the need for friends can refer to

Although this is a very basic thing, I am ashamed of it is not very understanding. So it's a bit warm.

Steps to write a file

    1. Open this file. If the file does not exist, create it.

    2. Create file locks to prevent concurrency

    3. Write this data to this file

    4. Release the lock and close the file handle

Steps to read a file

    1. Open this file and exit if you can't open it

    2. Reading data from a file

    3. Close this file

Open File operation

fopen($filename,$mode,$include_path,$context)

Open Failure returns False if open successfully returns the resource object.

Property name function
$filename The path of the file (the path of the file is separated by '/' to indicate that the ' \ ' may cause Linux not to be recognized)
$mode The form of file access (write
$include _path (optional) Find the location of the file starting from the specified directory
$context Optional. Specifies the environment for file handles. Context is a set of options that can modify the behavior of a stream.

Attention:
-When the file prefix is ftp://, which is the file server, the function will establish a passive mode connected to the specified server. and returns a pointer to the starting position of the file
-When the file prefix is http://, a pointer to the HTTP response to the specified server is established, returning a
-Perform file operations on Linux pay attention to the server's permissions configuration.

operator Schema name meaning
R Read mode Open the file from the beginning
r+ Read/write mode Open file, read and write from file header
W Write-only mode Open the file and start reading and writing from the file header. If the file exists, empty the contents of the file, if it does not exist, then wear the file
w+ Read/write mode Ditto
X Write carefully Returns False if the file exists
x+ Write carefully Open the file as read-write and return False if the file exists
A Additional Write at the end of a file
A + Additional Read and write operations at the end of a file

Write file operations

A. Need to call A fopen() function

1. ' Fwrite () ' describes ' int fwrite (resource $handle, string $string [, int $length]) ' $string   characters written  $length   limit the length of the Write Degree

B. No need to call fopen() function

1. File_put_contents () Description file_put_contents (File,data,mode,context) equivalent to one sentence of execution fopen (), fwrite (), fclose ()

Close File

Fclose ($FP) True if successful, and False otherwise

Read operation

$fp = fopen ('./test.php ', ' a ')//Open flock ($FP, lock_sh) in read-only mode;//read operation lock while (!feof ($fp)) {    $order = fgets ($fp);    echo $order}fclose ($FP)//Close File # feof ($FP)//detect if the file read to the end of file//read/write several operations */* reading a line of text */# fgets ($fp)  //Get the file line of characters # (More secure operation, filtered PHP and HTML tags) fgetss ($fp, ten, ' <a> ')//three parameters 1. File Resources 2. Limit the length of each fetch (for example, 0 does not limit the length) 3. Allowed Label # fgetcsv ($fp, 0, "/t ") with"/T "as a delimiter, split my array/* Read the entire file */# (do not need fopen () operation) ReadFile (string $filename [, bool $use _include_path = False [, resource $co NTEXT])  //Return file character length,!!! and output the content directly to the browser # fpassthru (resource $handle)  //Returns a Boolean value # file (string $filename [, int $flags = 0 [, Resource $con Text]]//Read the entire file into an array, save/* For each row as an element read one character */# fgetc ()/* Read any length character */# fread ($fp, $legth)

Other file functions

File_exists ("path");   Whether the file exists filesize ("path"); File size unlink;     deleting files

File lock

function: ' Fclock ($fp, ' Operation value ');//Open lock

Action Value meaning
Lock_sh Read operation lock, can be shared, other people can read the file
Lock_ex Write operation lock, mutual exclusion lock
Lock_un Release existing Locks
Lock_nb Prevents the plunger from occurring when the yoke is requested

Although this is a very basic thing, I am ashamed of it is not very understanding. So it's a bit warm.

Steps to write a file

    1. Open this file. If the file does not exist, create it.

    2. Create file locks to prevent concurrency

    3. Write this data to this file

    4. Release the lock and close the file handle

Steps to read a file

    1. Open this file and exit if you can't open it

    2. Reading data from a file

    3. Close this file

Open File operation

fopen($filename,$mode,$include_path,$context)

Open Failure returns False if open successfully returns the resource object.

Property name function
$filename The path of the file (the path of the file is separated by '/' to indicate that the ' \ ' may cause Linux not to be recognized)
$mode The form of file access (write
$include _path (optional) Find the location of the file starting from the specified directory
$context Optional. Specifies the environment for file handles. Context is a set of options that can modify the behavior of a stream.

Attention:
-When the file prefix is ftp://, which is the file server, the function will establish a passive mode connected to the specified server. and returns a pointer to the starting position of the file
-When the file prefix is http://, a pointer to the HTTP response to the specified server is established, returning a
-Perform file operations on Linux pay attention to the server's permissions configuration.

operator Schema name meaning
R Read mode Open the file from the beginning
r+ Read/write mode Open file, read and write from file header
W Write-only mode Open the file and start reading and writing from the file header. If the file exists, empty the contents of the file, if it does not exist, then wear the file
w+ Read/write mode Ditto
X Write carefully Returns False if the file exists
x+ Write carefully Open the file as read-write and return False if the file exists
A Additional Write at the end of a file
A + Additional Read and write operations at the end of a file

Write file operations

A. Need to call A fopen() function

1. ' Fwrite () ' describes ' int fwrite (resource $handle, string $string [, int $length]) ' $string   characters written  $length   limit the length of the Write Degree

B. No need to call fopen() function

1. File_put_contents () Description file_put_contents (File,data,mode,context) equivalent to one sentence of execution fopen (), fwrite (), fclose ()

Close File

Fclose ($FP) True if successful, and False otherwise

Read operation

$fp = fopen ('./test.php ', ' a ')//Open flock ($FP, lock_sh) in read-only mode;//read operation lock while (!feof ($fp)) {    $order = fgets ($fp);    echo $order}fclose ($FP)//Close File # feof ($FP)//detect if the file read to the end of file//read/write several operations */* reading a line of text */# fgets ($fp)  //Get the file line of characters # (More secure operation, filtered PHP and HTML tags) fgetss ($fp, ten, ' <a> ')//three parameters 1. File Resources 2. Limit the length of each fetch (for example, 0 does not limit the length) 3. Allowed Label # fgetcsv ($fp, 0, "/t ") with"/T "as a delimiter, split my array/* Read the entire file */# (do not need fopen () operation) ReadFile (string $filename [, bool $use _include_path = False [, resource $co NTEXT])  //Return file character length,!!! and output the content directly to the browser # fpassthru (resource $handle)  //Returns a Boolean value # file (string $filename [, int $flags = 0 [, Resource $con Text]]//Read the entire file into an array, save/* For each row as an element read one character */# fgetc ()/* Read any length character */# fread ($fp, $legth)

Other file functions

File_exists ("path");   Whether the file exists filesize ("path"); File size unlink;     deleting files

File lock

function: ' Fclock ($fp, ' Operation value ');//Open lock

Action Value meaning
Lock_sh Read operation lock, can be shared, other people can read the file
Lock_ex Write operation lock, mutual exclusion lock
Lock_un Release existing Locks
Lock_nb Prevents the plunger from occurring when the yoke is requested
Related recommendations:

PHP Namespaces and auto-loading

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.