Php Common File Operations Learning notes-PHP source code

Source: Internet
Author: User
In php, It is very convenient to perform operations on files, because php provides a large number of php file operation functions, next I will summarize the PHP file operation methods and functions. In php, It is very convenient to perform operations on files, because php provides a large number of php file operation functions, next I will summarize the PHP file operation methods and functions.

Script ec (2); script

1. Get the file name: basename ();

2. Obtain the directory where the file is located: dirname ();

3. Obtain the file information through pathinfo (). The returned result is an array, including the path, full name, file name, and extension. For example:

The Code is as follows:

$ File = '/com/netingcn/error. log ';
Print_r (pathinfo ($ file ));

Result:

Array (
[Dirname] =>/com/netingcn
[Basename] => error. log
[Extension] => log
[Filename] => error
)

4. Determine whether the file exists: is_file ();

5. Check whether the directory exists: is_dir ();

6. Determine whether a file or directory exists: file_exists ();

7. Read all the file content: file () or file_get_contents (). file () returns an array with a row of elements. file_get_contents () returns all the file content as a String;

8. Write the file fwrite, for example:

The Code is as follows:

$ Handler = fopen ($ file, 'w'); // w will overwrite the previous content, and a will be appended
Fwrite ($ handler, 'content ');
Fclose ($ handler); // remember to close the opened file handle 9. There are many File Read operations, which are described below:

$ Handler = fopen ($ file, 'R ');

While (! Feof ($ handler )){
$ Datas [] = fgets ($ handler); // read a row of content
}

While (! Feof ($ handler )){
$ Datas [] = fgetss ($ handler); // read a row of content and mark it in html
}

While (! Feof ($ handler )){
$ Datas [] = fgetcsv ($ handler); // read a row of content and parse the csv Field
}

$ Content = fread ($ handler, $ strLength); // read long-read characters

Fclose ($ handler );


The content of the PHP file is txt, And the 'list.txt 'file is stored in the same directory as the PHP file,

Note:

The content style of my txt file is as follows:

Email, FirstName, LastName...

Neil@ddd.com, neil, zhou...

...

The specific implementation is modified according to the actual situation.

The Code is as follows:

$ Fp = fopen ($ filePath, "r ");
$ A = read_content_to_array ($ fp); // obtain the content in the text file.
Fclose ($ fp );

/**
@ Desc read file content to array
@ Params $ fp: file resource
@ Return array
*/
Function read_content_to_array ($ fp ){
$ I = 0;
$ A = array ();
While (! Feof ($ fp) {// while LOOP, condition: not to the end of the file.


$ Buffer = fgets ($ fp, MAX_BYTES_PER_ROW); // The fgets () function is used to read files row by row from files, the number of bytes read. The default value is 1024 bytes. MAX_BYTES_PER_ROW is a custom constant, the number of bytes read


$ Block = explode (STR_TD_DIVIDER, $ buffer); // you can use a separator to split a row ($ buffer) and return an array consisting of strings. STR_TD_DIVIDER is a custom constant, the delimiter between fields in each line.


$ A [$ I] = $ block;
$ I = $ I + 1; // uses the while loop to increase the I value and build an array.
}
Return $;
}

PHP writes content to the txt file

The Code is as follows:

$ Handle = fopen ($ filePath, "w ");
Fwrite ($ handle, $ str );
Fclose ($ handle );

How about it? Does it look simple? The following is a summary of some simple knowledge:

Fopen ()
The fopen () function is used to open a file in PHP. The first parameter of this function contains the name of the file to be opened, and the second parameter specifies the mode used to open the file.

Mode description
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 object 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 ).


Feof ()
Detect End-of-file
The feof () function checks whether the object has reached the end of the object (EOF). The feof () function is useful when traversing data with unknown lengths cyclically. Note: In w, a, and x modes, you cannot read open files!
If (feof ($ file) echo "End of file ";

Fgets ()
The fgets () function is used to read objects row by row from objects. After this function is called, the file pointer is moved to the next row.

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.