PHP-based file operations-PHP Tutorial

Source: Internet
Author: User
Tags readfile
A detailed description of PHP file operations. Copy the code as follows: $ path1E: myphptext.txt; if (! File_exists ($ path1) {the echo file does not exist !;} Else {$ handle1fopen ($ path1, r +) orexit (Unabletoopenfile ); The 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 )."
";
//}
While (! Feof ($ handle1 )){
Echo fgetc ($ handle1 );
}
}


The code above illustrates a simple file read operation. Note:
Fopen is used to open file resources.
Usage:
$ 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.
Fgetc:
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.

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

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

The code is as follows:


If(file_exists(“hello.txt "))
{
Echo "file exists ";
}
// Open the file


Format:
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
Format:
Fwrite (resource, string );
Note: add the specified content to the opened file.
Resource: open file
String: the content to be written.
Example:
$ Handle = fopen(“hello.txt "," w ") // If a, data can be appended.
Fwrite ($ handle, "1 \ r \ n ")

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

// Read a row of data
Format:
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;
Example:
$ Handle = fopen(“hello.txt "," r ");
$ Buffer = fgets ($ handle, 1024 );
Echo $ handle; // output a line of information

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

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

// Delete an object
Format:
Unlink ()
Description: If a file is deleted successfully, true is returned. otherwise, false is returned.
Example:
Unlink(cmdb.txt ")

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

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

// Get the file name
Format:
Basename (filepath)
Returns the file name from the specified path.
Example:
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
Format:
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
Format:
Copy (source, dest)
Note: copy the source file to dest.
Example: copy(“h.txt "," newfloder \ a.txt ")

// Determine whether it is a directory
Format:
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.
Example:

The 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.
Example:
$ Hand = opendir (".") // open the root directory

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

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

The http://www.bkjia.com/PHPjc/327764.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327764.htmlTechArticle 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 ");/...

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.