PHP file-based operations

Source: Internet
Author: User
This article provides a detailed analysis of PHP file operations. For more information, see

This article provides a detailed analysis of PHP file operations. For more information, see

Knowledge Point introduction:
1. Determine whether a file or directory has a bool

The Code is as follows:


File_exists (string filename)


2. Get the file name

The Code is as follows:


Basename (filepath)


3. open the file

The Code is as follows:


Fopen (filename, mode)


4. Get file path information

The Code is as follows:


Pathinfo (path)


5. Write files

The Code is as follows:


Fwrite (resource, string)


6. Take the absolute path

The Code is as follows:


Realpath (filename)


7. close the file

The Code is as follows:


Fclose ($ handle)


8. Copy an object

The Code is as follows:


Copy (source, dest)


9. Read a row of data

The Code is as follows:


Fgets (int handle [, int length])


10. Determine if it is a directory

The Code is as follows:


Is_dir (filename)


11. Read the entire file

The Code is as follows:


Readfile (filename)


12. Open the Directory

The Code is as follows:


Opendir (path)


13. Get the file size

The Code is as follows:


Filesize (filename)


14. Read the Directory

The Code is as follows:


Readdir ($ handle)


15. delete an object

The Code is as follows:


Unlink ()


16. Disable directory

The Code is as follows:


Closedir ($ handle)


17. Create a directory

The Code is as follows:


Mkdir (dirname)


18. delete a directory

The Code is as follows:


Unlink ()


19. Determine whether a file or directory exists

The Code is as follows:


Bool file_exists (string filename)


20. check whether a file or directory exists. If the website space exists, the system returns true. Otherwise, the system returns false.
Format:

The Code is as follows:


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


Open a file

Format:
Fopen (filename, mode)
Description: 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 files

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 file

Format:
Fclose ($ handle)
Disable opened files
Example:

The Code is as follows:


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

The Code is as follows:


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

The Code is as follows:


Readfile(‑hello.txt ");
?>



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 directory

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

Delete directory

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

Get File Name

Format:
Basename (filepath)
Returns the file name from the specified path.
Example:
Basename ("c: \ mytools \ a.txt") // returns a.txt

Get file path information

Pathinfo (path)
Returns the file path information. The result is saved in an array. The website space is indicated by the subscript of the array.
Dirname (PATH), basename (file name), extension (extension)
Example: pathinfo ("c: \ mytools \ a.txt ")

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 a file

Format:
Copy (source, dest)
Note: copy the source file to dest.
Example: copy(“h.txt "," newfloder \ a.txt ")

Determine if it is a directory

Format:
Is_dir (filename)
Determines whether a given file name is a directory. If filename exists and
Is the directory, Hong Kong server, returns true, Otherwise returns false.
Example:

The Code is as follows:


If (is_dir ("newfolder "))
{
Echo "is a file directory ";
}



Open Directory

Format: opendir (path)
Description: open a specified file directory and return a resource identifier.
Example:
$ Hand = opendir (".") // open the root directory

Read directory

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

Close directory

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

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.