There are many File operation functions in php. next I will introduce to you how to use common file operation functions in php. 1. get the file name: basename (); 2. get the object
There are many File operation functions in php. I will introduce you to the usage of common file operation functions in php.
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.
The instance 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 instance 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 );