Learning notes for common php file operations

Source: Internet
Author: User
Tags constant php file

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: Copy code

$ 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: Copy code

$ 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: Copy code

$ 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: Copy code

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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.