Common functions and constants in php files
Constants for file operations
The following constant is the most commonly used. Is a constant that is the delimiter of the file directory.
Let’s take a look at the format:
Platform | Separator |
---|---|
windows | \ |
unix-like | / |
The path format of windows is d:\xxx\xxx Note: windows supports d:/xxx/xxx
The path format of linux is /home/xxx/xxx Note: If \home\xxx\xxx is on linux It's wrong
So when you enable escaping and the like, the escape character \ is the same as d:\xxx\xxx if used together. When judging, if there are two \, convert it into one \ and replace the \ with / to split the path, so that the paths on Linux or Windows can remain unified.
We will use a constant:
DIRECTORY_SEPARATOR
Let’s write a small example to define the path of the current file:
Since FILE is a predefined constant of PHP, it cannot be changed. If necessary, FILE can also adapt to the operating system.
Then don’t use FILE, you can use custom constants and process FILE as follows:
<?php $_current_file = str_replace(array('/', '\'), DIRECTORY_SEPARATOR, __FILE__); define('__CUR_FILE__', $_current_file); echo __CUR_FILE__; ?>
file Pointer operation function
rewind (resource handle)
Function: The pointer returns to the beginning
fseek ( resource handle, int offset [, int from_where])
Function: Move the file pointer backward by the specified character
In the previous reading, we found that fread reads data of the specified length. Read the content of the specified length. The next time you read it, start from the original position and then read backward.
As shown in the picture above, we can imagine:
1. When the file is first opened, the red icon is read
2 .The false color of the file is read from A to C
3. The next time you open it, you can start reading from the green arrow of C.
We write a batch of files in the demo.txt file:
abcdeefghijklk
opqrst
uvwxyz
12345678
We can start experimenting once.
<?php $fp = fopen('demo2.txt', 'r+'); //讀取10個字符 echo fread($fp,10); //指針設置回到開始處 rewind($fp); //再讀取10次看看輸出的是什么 echo '<br>'; echo fread($fp,10); echo '<br>'; //文件指針向后移動10個字符 echo fseek($fp,10); echo '<br>'; //再看看文件中輸出的是什么 echo fread($fp,10); echo '<br>'; fclose($fp); ?>
In the above example, you will find that fseek will move as many bytes as the specified length. And rewind returns to the beginning of the file every time.
How to move to the end? We can count the number of bytes. Move directly to the back during fseek.
Let’s talk about filesize statistics bytes.
filesize Detect the size of the file
<?php $filename = 'demo.txt'; echo $filename . '文件大小為: ' . filesize($filename) . ' bytes'; ?>
Other functions for operating files
In fact, there are some other functions for operating files , read file
Function name | Function |
---|---|
Read the entire file Into an array | |
Read a line from the file pointer, read to the end and return false | |
Read a character from the file pointer and return false after reading to the end | |
Truncate the file to the given length |
abcdeefghijklkopqrst
uvwxyz
12345678
<?php //以增加的r模式打開 $fp = fopen('demo.txt','r+'); //你分發(fā)現每次只讀一個字符 echo fgetc($fp); //我要全部讀取可以,讀取一次將結果賦值一次給$string while($string = fgetc($fp)){ echo $string; } ?>fgets opens one line at a time:
<?php //以增加的r模式打開 $fp = fopen('demo.txt','r+'); //你分發(fā)現每次只讀一個字符 echo fgets($fp); echo fgets($fp); echo fgets($fp); echo fgets($fp); ?>With the above code, you will find that one line is opened at a time each time it is read. The final read return is false. Let’s look at the file interception function next:
<?php //打開我們上面的demo.txt文件 $file = fopen("demo.txt", "a+"); //你可以數數20個字有多長,看看是不是達到效果了 echo ftruncate($file,20); fclose($file); ?>In the above example, we found that the content can be displayed as long as it is intercepted.
File time function
Function description | |
---|---|
File creation time | |
File modification time | |
File last access time |