国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

read file

readfile reads a file

So how to read a file? Let's learn a function first.

int readfile (string $filename)

Function: Pass in a file path and output a file.

In the following code, the file is read as long as the file name or the specified file path is passed in.

<?php
    //linux類的讀了方式
    readfile("/home/paul/test.txt");
    //windows類的讀取方式
    readfile("c:\boot.ini");
?>

Note: The windows slash in the above code is \slash, which may escape some characters. Therefore, when we write, we write two slashes.

file_get_contentsopen file

The above is a direct output after simply opening the file. Is there any operation method that can be assigned to a variable after opening the file?

PHP will certainly provide this method. This method is one of the ways PHP opens a file and returns the content. Let's take a look at the function:

string file_get_contents (string filename)

Function: Pass in a file or file path to open this File returns the contents of the file. The content of the file is a string.

<?php
 
    $filename = 'NoAlike.txt';
 
    $filestring = file_get_contents($filename);
    echo $filestring;
?>

The above code opens a file and outputs the contents of the file.

Let’s expand the code based on the previous knowledge. Use your previous knowledge.

<?php
    //假設(shè)我們有一個(gè)多行的文件叫NoAlike.txt,沒(méi)有的話你可以新建一個(gè)這個(gè)文件
     $filename = 'NoAlike.txt';
 
 
    //打開這個(gè)文件,將文件內(nèi)容賦值給$filestring
    $filestring = file_get_contents($filename);
 
    //因?yàn)槊恳恍杏幸粋€(gè)回車即\n,我用\n來(lái)把這個(gè)字符串切割成數(shù)組
    $filearray = explode("\n", $filestring);
 
    //把切割成的數(shù)組,下標(biāo)賦值給$key,值賦值給$val,每次循環(huán)將$key加1。
    while (list($key, $val) = each($filearray)) {
        ++$key;
        $val = trim($val);
 
        //用的單引號(hào),單引號(hào)不解釋變量進(jìn)行了拼接而已
        print 'Line' . $key .':'.  $val.'<br />';
    }
?>

Above, we have combined the knowledge we have learned before.

fopen, fread, and fclose operations read files

The above file_get_contents method of opening files is simple and crude. The following

resource fopen (string $filename, string mode)

string fread (resource $operation resource, int read length)

bool fclose (resource $operation Resources)

Through the above functions, we will explain the usual operation methods of resource types:

1. Open resources

2. Use related functions to operate

3. Close resources

fopen function The function of the fopen function is to open a file. There are two main parameters:

1. The path to open the file

2. The path to open the file Pattern

The return type is a resource type. We first encountered the resource type mentioned in the previous basic type.
The resource type requires other functions to operate this resource. All resources must be closed when they are opened.

fread function The function of the function is to read the open file resource. Read the file resource of the specified length, read part of it and move part backward. to the end of the file.

fclose function The function of the fclose function is to close resources. Resources are opened and closed.

After understanding the functions, the last two functions are relatively simple. What are the modes of the fopen function? The modes of fopen are as follows. Let’s talk about the modes of fopen:


QQ截圖20161009101723.png


##

Next, we will only learn the r mode. At the end of the lesson, we will talk about several other modes when writing.

We must first know how to read files before we can master writing files well.

1.Open the file

<?php
    //你可以創(chuàng)建一個(gè)NoAlike.txt,以只讀模式打開
    $fp = fopen('NoAlike.txt', "r");
 
 
    //var_dump()操作一下$fp看看效果,輸出的是不是只有類型提示的是resource
    var_dump($fp);
?>

2.Read the file

<?php
    $fp = fopen('NoAlike.txt', "r");
 
    //打開一個(gè)文件類型后,讀取長(zhǎng)度
    $contents = fread($handle, 1024);
?>

3.Close the file

<?php
    $fp = fopen($filename, 'r');
    $contents = fread($fp, 1024);
    fclose($fp);
    echo $contents;
?>

Other notes:


QQ截圖20161009101707.png


##Usage examples:

<?php
 $fp = fopen($filename, 'ab');
 $contents = fwrite($fp, '可愛(ài)的很\n喲');
 fclose($fp);
 echo $contents;
 ?>

Note:

The experiment cannot allow the naked eye to see the effect of this experiment. Just remember this feature.

Windows provides a text conversion tag ('t') that can transparently convert \n to \r\n.

Correspondingly, you can also use 'b' to force binary mode so that the data will not be converted. To use these flags, use either 'b' or 't' as the last character of the mode argument.


Continuing Learning
||
<?php //假設(shè)我們有一個(gè)多行的文件叫NoAlike.txt,沒(méi)有的話你可以新建一個(gè)這個(gè)文件 $filename = 'NoAlike.txt'; //打開這個(gè)文件,將文件內(nèi)容賦值給$filestring $filestring = file_get_contents($filename); //因?yàn)槊恳恍杏幸粋€(gè)回車即\n,我用\n來(lái)把這個(gè)字符串切割成數(shù)組 $filearray = explode("\n", $filestring); //把切割成的數(shù)組,下標(biāo)賦值給$key,值賦值給$val,每次循環(huán)將$key加1。 while (list($key, $val) = each($filearray)) { ++$key; $val = trim($val); //用的單引號(hào),單引號(hào)不解釋變量進(jìn)行了拼接而已 print 'Line' . $key .':'. $val.'<br />'; } ?>
submitReset Code