Php file operations-notes for common file operations

Source: Internet
Author: User
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:

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

Result:

  1. Array (
  2. [Dirname] =>/com/netingcn
  3. [Basename] => error. log
  4. [Extension] => log
  5. [Filename] => error
  6. )

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:

  1. $ Handler = fopen ($ file, 'w'); // w will overwrite the previous content, and a will be appended
  2. Fwrite ($ handler, 'Content ');
  3. Fclose ($ handler); // remember to close the opened file handle 9. There are many file read operations, which are described below:
  4. $ Handler = fopen ($ file, 'r ');
  5. While (! Feof ($ handler )){
  6. $ Datas [] = fgets ($ handler); // read a row of content
  7. }
  8. While (! Feof ($ handler )){
  9. $ Datas [] = fgetss ($ handler); // read a row of content and mark it in html
  10. }
  11. While (! Feof ($ handler )){
  12. $ Datas [] = fgetcsv ($ handler); // read a row of content and parse the csv field
  13. }
  14. $ Content = fread ($ handler, $ strLength); // read long-read characters
  15. Fclose ($ handler );

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.