PHP file-based operations

Source: Internet
Author: User

Copy codeThe Code is as follows: $ path1 = "E:/myphp/text.txt ";
If (! File_exists ($ path1 )){
Echo "the file does not exist! ";
} Else {
$ Handle1 = fopen ($ path1, 'r + ') or exit ("Unable to open file ");
// While (! Feof ($ handle1 )){
// Echo fgets ($ handle1). "<br> ";
While (! Feof ($ handle1 )){
Echo fgetc ($ handle1 );

The code above illustrates a simple file read operation. Note:
Fopen is used to open file resources.
$ File = fopen ("welcome.txt", "r ");
Specifically, the first parameter is the file path. The following parameters are the methods required to open a file:
R read-only. Start at the beginning of the file.
R + read/write. Start at the beginning of the file.
W write only. Open and clear the file content. If the file does not exist, create a new file.
W + read/write. Open and clear the file content. If the file does not exist, create a new file.
A append. Open and write to the end of the file. If the file does not exist, create a new file.
A + Read/append. Write content to the end of the file to keep the file content.
X write only. Create a new file. If the file already exists, FALSE is returned.
X +
Read/write. Create a new file. If the file already exists, FALSE and an error are returned.

NOTE: If fopen () cannot open the specified file, 0 (false) is returned ).
The first four are commonly used.
String fgetc (resource $ handle)
Returns a string containing a character, which is obtained from the file pointed to by handle. If EOF is encountered, FALSE is returned.

String fgets (int $ handle [, int $ length])
Read a row from the file pointed to by handle and return a string of up to length-1 bytes. When a line break (including the returned value), EOF, or the length-1 byte is read, it is stopped ). If length is not specified, the default value is 1 K, or 1024 bytes.
If an error occurs, FALSE is returned.

String fgetss (resource $ handle [, int $ length [, string $ allowable_tags])
Similar to fgets (), only fgetss tries to remove any HTML and PHP markup from the read text. (Same as fgets (), it only filters html and php tags .)
You can use the optional third parameter to specify which tags will not be removed.
The feof () function checks whether it has reached the end of the file (eof ).

// Determine whether a file or directory exists
Bool file_exists (string filename)
Determines whether a file or directory exists. If yes, true is returned. Otherwise, false is returned.

Copy codeCode: if(file_exists(“hello.txt "))
Echo "file exists ";
// Open the file

Fopen (filename, mode)
Note:Open a specified file in the specified format
Filename: name of the file to be opened
Mode:Open Mode
Fopen(“hello.txt "," w ");
Open the hello.txt file in the written format.

// Write an object
Fwrite (resource, string );
Note: add the specified content to the opened file.
Resource: Open File
String: the content to be written.
$ Handle = fopen(“hello.txt "," w ") // If a, data can be appended.
Fwrite ($ handle, "1 \ r \ n ")

// Close the file
Fclose ($ handle)
Disable opened files
$ Handle = fopen(“hello.txt "," w ");
Fclose ($ handle );

// Read a row of data
Fgets (int handle [, int length])
Description: Read length-1 characters. If length is not specified, the default byte is 1 kb,
If a newline, EOF, or has read length-1 characters, the program is terminated,
False is returned when an error occurs;
$ Handle = fopen(“hello.txt "," r ");
$ Buffer = fgets ($ handle, 1024 );
Echo $ handle; // output a line of information

// Read the entire file
Readfile (filename)
Description: reads the entire file and outputs it to the browser.
Readfile(‑hello.txt ");

// Obtain the file size
Filesize (filename)
Description: gets the specified file size. If an error occurs, false is returned.
Filesize(+a.rar ")

// Delete an object
Unlink ()
Description: if a file is deleted successfully, true is returned. Otherwise, false is returned.
Unlink(cmdb.txt ")

// Create a directory
Mkdir (dirname)
Creates a directory.
Example: mkdir ("newfolder"); // create a new folder in the current directory

// Delete the Directory
Rmdir (dirname)
Deletes a directory.
For example, rmdir ("newfolder ");

// Get the file name
Basename (filepath)
Returns the file name from the specified path.
Basename ("c: \ mytools \ a.txt") // returns a.txt

// Obtain the file path information
Pathinfo (path)
Description: The returned file path information is saved in the array, and the subscript of the array is
Dirname (PATH), basename (file name), extension (extension)
Example: pathinfo ("c: \ mytools \ a.txt ")

// Obtain the absolute path
Realpath (filename)
Description: gets the absolute path of the specified file. If the path fails, false is returned.
Example: realpath(“h.txt ") // F: \ apache \ example \ h.txt

// Copy an object
Copy (source, dest)
Note: copy the source file to dest.
Example: copy(“h.txt "," newfloder \ a.txt ")

// Determine whether it is a directory
Is_dir (filename)
Determines whether a given file name is a directory. If filename exists and
If it is a directory, true is returned; otherwise, false is returned.

Copy codeThe Code is as follows: if (is_dir ("newfolder "))
Echo "is a file directory ";

// Open the Directory
Format: opendir (path)
Description: open a specified file directory and return a resource identifier.
$ Hand = opendir (".") // open the root directory

// Read the Directory
Readdir ($ handle)
Description: reads an open file directory stream.
Readdir ($ hand );

// Close the Directory
Closedir ($ handle)
Disables an Open Directory stream.
Example: closedir ($ hand );

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: 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.