Home >Backend Development >PHP Tutorial >PHP Advanced Features 2: File Processing
File processing in PHP is also a very important module. The main content of this article is an introduction to the file system in PHP.
Purpose of file system
1. Project processing is inseparable from file processing
2. Files can be used to save data for a long time
3. Create cache and perform file operations in the server
File system functions Usage details
1. Basic judgment function
is_dir — judge whether the given file name is a directory
is_file — judge whether the given file name is a file
is_executable — judge whether the given file name is executable
is_link — Determine whether the given file name is a symbolic link
is_readable — Determine whether the given file name is readable
is_uploaded_file — Determine whether the file was uploaded through HTTP POST
is_writable — Determine the given file name Whether it is writable
is_writeable — an alias of is_writable
2. Obtain file-related information
file_exists — check whether the file or directory exists
fileatime — get the last access time of the file
filectime — get the inode of the file Modification time
filegroup — Get the group of the file
fileinode — Get the inode of the file
filemtime — Get the file modification time
fileowner — Get the owner of the file
fileperms — Get the permissions of the file
filesize — Get the file size
filetype — Get the file type
Let’s write an example below, pass in the file name and print its details.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
functiongetFileInfo($ filename){ if(!file_exists($filename)){ with with ).'doesn't exist'; }if( is_file($filename)) {is a file'; 'is a directory' ; $filename)){
echo $ filename.. $filename.' is not an executable file'; if(is_readable($filename)) { hecho $ Filename.. '' is readable '; }} Lse { echo$filename.' is not readable'; ($filename)){ is writable'; }} Lse { echo $ Filename.. echo 文 'file' . $Filename. 'The size is' .getFilesize (Filesize ( $ Filename) .''; ; ; ; ; ; The type of $filename.' is '.filetype( $filename).''; echo'File'.$filename.' The owner of is '.fileowner($filename).''; The last access time of file '.$filename.' is '.getTime(fileatime($filename) .''; ode is '.fileinode($filename) .''; The modification time of 'file'.$filename.' is' .getTime(filemtime($filename)).''; echo'The permissions of file '.$filename.' are '.fileperms($filename).''; } functiongetTime($ time){ return date( ,$time ; } functiongetFileSize ($size){
($size>=pow(2,40)){ ($size/pow(2, ),2); $dw ='PB'; size>=pow(2, 30)){ $size=round($size/pow(2,30),2); $dw='TB'; }elseif($size> ;=pow(2,20)){ $size=round($ size/pow(2,20),2); $dw ='GB'; }elseif($size>=pow (2,10) ){ $size=round($ size/pow(2,10 $$$ dw='MB'; } return $size.$dw; } getFileInfo ('1.php'); Run results 1.php is a file 3. File path related functions Relative path: relative to the upper and lower directories of the current directory . Current directory Path separator symbol linux/Unix “/” Absolute path: It can refer to the root of the operating system, or it can refer to the document root directory where the website is stored If it is executed in the server (executed through PHP file processingfunction), the path "root" refers to The root of the operating system file name part of the path 1 ; $url2= ; $url3= ; echo basename $url1); echo basename ($url2); echo ($url3); echo ($url4); Running results index.php It can be seen that the basename function returns the name of the file, which is the last item. $url1 ="./aaa/bbb/index.php"; $url2="../www/yyy/login.rar"; $url3="c:/appserv/www/demo.html"; $ url4="http://localhost /yyy/www.gif"; echo dirname(dirname($url1)); echo dirname($url2); echo dirname( $url3); echo dirname($url4); Running results ./aaa You can find that the dirname function can be used in multiple levels of nesting. What is returned is the path where it is located, that is, all items except the last item. file name and extension Copy a file 1 touch("./php.apahce");//Create file unlink("C:/AppServ/www/xsphp/apache. php"); //Delete file rename("./test.txt","d:/test2.txt"); //Rename the file copy("cache.txt","./cache5.txt"); //Copy file chmod("a.txt",755); //Set file permissions Permission related content rwx represents the owner of this file r read w write Read w write 1 = file_get_contents( );echo$str; ?> directly opens the content in the 1.txt file and returns the text information in the file. 2. file_put_contents(filename,content) 1 file_put_ contents ("2.txt", );?> 2.txt file will be created if it does not exist. file and write the character abcd String, returns 4, which is the length of the string. If the file exists, the file will be emptied, then the string will be written, and the write length will be returned. 1 5 = file( );var_dump($str); echocount( $str) ?> You can get the row content in the form of an array and output the number of rows. Disadvantage: The content of the specified part cannot be read. 4.fopen(filename,mode) filename is the file name, which can be a path plus name or a remote server file. mode is the way to open a file r, open the file in read-only mode Note: r+ has read and write attributes, starts writing from the beginning of the file, and retains the content that has not been overwritten in the original file; w+ has read and write attributes, if when writing If the file exists, it will be cleared and written from scratch. Returns a file resource 5.fwrite(file,content) File writing function, file is the file resource, obtained with the fopen function, content is the writing content. Same as fputs function. For example php $file=fopen("1.txt","r+"); $result =fwrite($file,"xx"); if ($result){ echo"Success"; "Failed"; } ?> then write the resource from scratch, that is, set the first two characters to xx6. fread(file,size)read The length of the specified part of the file, file is the file resource, the object returned by fopen, and size is the length of the read characters. For example 2 3 4 5 ; $content=fread ($file,filesize ("1.txt")); echo$content ;?> ; However, the above filesize method can only obtain the local file size, and a different method is needed to read remote files. For example $file=fopen("http://www.qq.com","r"); $str =""; while(!feof($file)){ / /Judge time to end of file $str.=fread($file,1024); } echo$str; ?> 7.f gets(file) file is a file resource, read every time Take a row. For example, we read how many lines there are in Tencent's homepage. $file=fopen("http://www.qq.com","r"); $str=""; $count=0; while(! feof($file)){ $str.=fgets($file ); $count++; } echo $count; ? > The result will be 8893. We can check the source file to see how many lines it has in total and just verify it. 7.fgetc(file) is very similar to the fgets method. file is a file resource and reads characters each time. For example, we read how many characters there are in Tencent's homepage. $file=fopen("http://www.qq.com","r"); $str=""; $count=0; while(! feof($file)){ $str.=fgetc($file ); $count++; } echo $count; ? > The above code will output all the number of characters. 8.ftell(file) ftell returns the pointer position of the currently read file, file is the file resource, and is the object returned by fopen. 9.fseek(file,offset,whence) file The file system pointer is a resource typically created by fopen(). offset Offset. To move to the position before the end of the file, you need to pass a negative value to offset and set whence to SEEK_END. whence SEEK_SET – Set position equal to offset bytes. SEEK_CUR – Set the position to the current position plus offset. SEEK_END – Set the position to the end of the file plus offset. 10.rewind($file) Back to the file header, file is a file resource For example php $file =fopen("1.txt","r"); echoftell($file )." echofread( $file, 10)." echo ftell($file)." fseek ($file,20,SEEK_CUR);//The current pointer moves forward 20 units echoftell($ file)." echof read($file ,10)." echoftell ($file)." f seek( $file,-20,SEEK_END);//Move the pointer to the first 20 characters of the end of the file echo ftell($file)." echofread ($file,10)." echo ftell($file)." rewind($file);//Return to the head of the file echoftell($file)." > 840 operation 4 5 6 7 php if(flock($file,LOCK_EX )){ fwrite($file,"xxx"); ($file,LOCK_UN ); ?>
The above has introduced PHP advanced feature 2: file processing, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.
|