PHP

Detailed explanation based on PHP file operation


Knowledge Introduction: 1. Determine whether bool exists in the file or directory

file_exists(string filename) 

2. Get the file name

basename(filepath) 

Open the file

fopen(filename,mode) 

4. Get file path information

pathinfo(path) 

5. Write files

fwrite(resource,string) 

Take the absolute path

realpath(filename) 

Close the file

fclose($handle) 

8. Copy files

copy(source,dest) 

9. Read 1 row

fgets(int handle[,int length]) 

10. Determine if it is a directory

is_dir(filename) 

Read the entire file

readfile(filename)

Open the directory

opendir(path) 

13. Get the file size

filesize(filename) 

14. Read directories

readdir($handle) 

15. Delete files

unlink()

Close the directory

closedir($handle) 

17. Create a directory

mkdir(dirname)   

Delete directories

unlink()   

19. Determine if a file or directory exists

basename(filepath) 

8 20. Determine whether a file or directory exists. If it exists, it returns true; otherwise, it returns false Format:

basename(filepath) 

9 Open the file

Format: fopen(filename,mode) Description: Opens the specified file in the specified format filename: The file name to open mode: Turn on mode fopen (” hello txt ”, “w”); Means to open the hello.txt file as a write

Write files

Format: fwrite(resource,string); Description: Adds the specified content to an open file resource: Open file string: What to write Ex. : $handle = fopen(” hello. txt ”, “w”) // If a, the data can be appended fwrite ($handle, “1 \ r \ n”)

Close the file

Format: fclose($handle) Note: Close open files Ex. :

fopen(filename,mode) 

0

Read 1 row

Format: fgets(int handle[,int length]) Note: Read length-1 characters. If length is not specified, the default byte is 1KB, If a line feed, EOF, or length-1 character has been read, the program terminates, Returns false on error; Ex. :

$handle = fopen( " hello.txt " , " r " );
$buffer = fgets($handle,1024);
 echo $handle; // The output 1 Line information

Read the entire file

Format: readfile(filename) Read the entire file and output it to the browser Ex. :

 <?
 readfile( " hello.txt " );
 ?>

Fetch file size

Format: filesize(filename) Description: Gets the specified file size, error returns false Ex. : filesize (” a. rar ”)

Delete the file

Format: unlink() Note: Delete 1 file, return true on success, otherwise return false Ex. : unlink (” b. txt ”)

Create a directory

Format: mkdir(dirname) Description: Create 1 directory Example: mkdir (” newfolder ”); // Create a new folder under the current directory

Delete the directory

Format: rmdir(dirname) Note: Delete 1 directory Example: rmdir (” newfolder ”);

Get file name

Format: basename(filepath) Returns the filename from the specified path Ex. : basename(” c:\mytools\ a.txt ”) // Return a.txt

Gets file path information

pathinfo(path) Returns file path information, the result is saved in an array, the array index is dirname(path), basename (file name), extension (extension) Example: pathinfo (” c: \ mytools \ a txt ”)

Take the absolute path

Format: realpath(filename) Description: Takes the absolute path of the specified file, returns false on failure Example: realpath (” h. txt ”) / / F: \ apache \ example \ h txt

Copy the 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, returns true, otherwise returns false. Ex. :

 if(is_dir( " newfolder " ))
{
 echo  "File directory";
}

Open directory

Format: opendir (path) Description: Opens a specified file directory and returns a resource identifier Ex. : $hand = opendir(”. ”) // Open root

Read the directory

Format: readdir($handle) Read 1 open file directory stream readdir($hand);

Close the directory

Format: closedir($handle) Description: Close 1 open directory stream Example: closedir ($hand);