PHP file Operations

Source: Internet
Author: User
Tags array example flock

It is often necessary to create so-called handles before processing the contents of a file. After you finish the file operation, you should destroy the handle.
to write data to a file, there are typically 3-step operations:
1) Open the file. If the file does not exist, you need to create the file first. 2) write the data to the file. 3) Close the file
Reading data from a file also requires three steps:
1) Open the file. If the file cannot be opened, it should exit correctly. 2) Read the data from the file. 3) Close the file

fopen () function, generally need to pass the four-to-five parameters. Typically 2 parameters are used, the first parameter is the URL to open the file, and the second argument is open.
The first parameter can be a local file address or a network file address.
Fclose () Close file
The fread (int $handle, int $length) $handle is the data source obtained through fopen () $length is the length of the specified file to read.
File (int $handle) The function returns an array in which each element is a line of the file, separated by a newline character, and the newline character is appended to the end of each element.
Fpassthru () Prints the information standard in the file to the browser and closes it after the output. The operation returns true successfully, otherwise false is returned.
Fgets (int $handle, 100) read a line in the document
FGETSS () differs from the fgets () function to filter strings that contain PHP and HTML tags
Fgetcsv () returns the array example according to a specific symbol, separating the text information from the file: Fgetcsv ($handle, 100, "-")

Write file operation
Fwrite ($file, $STR) The first parameter is the file resource that needs to be written, the second argument is the string to be written
File_get_contents () Read all the contents of a file at once
File_put_contents () writes the file contents two parameters, the first parameter is the target file name, and the second parameter is the content to write. When we want to write the file in an append way, we need to pass the third parameter to the value file_append.

Other common processing functions
Copy (string source,string dest) copies the file source to Dest
DirName (string path) where path is a string that points to the full path of a file, returns the directory where the file resides
File_exiets (string filename) file exists
Feof (resource Handel) file pointer points to EOF (End of file) or error, returns true
File_size (string filename) returns the number of bytes in the file size
Ftell () View the current location of the file pointer

File lock
Flock ($file, lock_sh) The first parameter is the operation
Lock_sh read operation lock. means that the file can be shared and other people can read the file
LOCK_EX write Operation lock. This is mutually exclusive. The file cannot be shared
Lock_nu Releasing existing locks
LOCK_NBprevent blocking when requesting locking
The flock () function is no longer available for NFS or other network file systems. It also cannot be used in other file systems that do not support file locking, such as fat. In some operating systems, it is implemented at the process level, so if you use it in the multithreaded Server API, the function is not used correctly.

Related Article

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.