Constants for common file functions

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:


QQ截图20161009110829.png


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 wrong on Linux
So when If you enable escaping, etc., if the escape characters \ are used together, d:\xxx\xxx will be the same. 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 PHP’s default Constants are defined, so there is no way to change them. 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: The file pointer moves backward Specified characters

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.

                                                                                                                                        . The false color of the file is read from A to CQQ截图20161009110843.png

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 an experiment .

<?php
$fp = fopen('output.txt', 'r+');
//读取10个字符
echo fread($fp,10);
 
//指针设置回到开始处
rewind($handle);
//再读取10次看看输出的是什么
echo fread($fp,10);
 
//文件指针向后移动10个字符
echo fseek($fp,10);
 
//再看看文件中输出的是什么
echo fread($fp,10);
 
fclose($handle);
?>

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 detects 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 the file


##

We use an example to use all the above functions.

We write a batch of files in the demo.txt file:

abcdeefghijklk
opqrst
uvwxyz
12345678

<?php
 
//以增加的r模式打开
$fp = fopen('demo.txt','r+');
 
//你分发现每次只读一个字符
echo  fgetc($fp);
 
//我要全部读取可以,读取一次将结果赋值一次给$string
while($string = fgetc($fp)){
 
    echo $string;
 
}
?>

fgets opens one line at a time :

<?php
 
//以增加的r模式打开
$fp = fopen('demo.txt','r+');
 
//你分发现每次只读一个字符
echo  fgets($fp);
echo  fgets($fp);
echo  fgets($fp);
echo  fgets($fp);
 
?>

With the above code, you will find that each read opens one line at a time. 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.

QQ截图20161009111024.png

<?php
 
$filename = 'demo.txt';
 
if (file_exists($filename)) {
    echo "$filename文件的上次访问时间是: " . date("Y-m-d H:i:s", fileatime($filename));
 
    echo "$filename文件的创建时间是: " . date("Y-m-d H:i:s", filectime($filename));
 
     echo "$filename文件的修改时间是: " . date("Y-m-d H:i:s", filemtime($filename));
}
?>


Continuing Learning
||
<?php $filename = 'demo.txt'; if (file_exists($filename)) { echo "$filename文件的上次访问时间是: " . date("Y-m-d H:i:s", fileatime($filename)); echo "$filename文件的创建时间是: " . date("Y-m-d H:i:s", filectime($filename)); echo "$filename文件的修改时间是: " . date("Y-m-d H:i:s", filemtime($filename)); } ?>
submitReset Code