How to store all data of a file into a string in php

青灯夜游
Release: 2023-03-11 22:26:01
Original
3656 people have browsed it

In the previous article, we introduced the method of reading the entire file at one time and obtaining all the data. If you are interested, you can click on the link to view "php file operation: how to obtain all the data of the file at one time 》. Today we will introduce another method of reading the entire file at one time. You can refer to it if necessary.

The method I will introduce to you today can not only read the entire file at one time, but also store the read file content into a string. Isn’t it amazing? This requires the use of a PHP built-in function file_get_contents(). Let’s learn about this function today.

Let’s take the “test.txt” file as an example. The contents of the file are:

How to store all data of a file into a string in php

Let’s first take a look at how to use the file_get_contents() function Read the file and store the contents into a string.

<?php
header("Content-Type: text/html;charset=utf-8");    //设置字符编码
$file = &#39;test.txt&#39;;
$filestr = file_get_contents($file);
if($filestr){
  echo $filestr;
}else{
  echo &#39;读取失败!&#39;;
}
?>
Copy after login

Output result:

How to store all data of a file into a string in php

It can be seen that there is no need to open or close the file operation, just use "file_get_contents($file )" statement can read the entire file at once. This is very similar to the readfile() function introduced before, but they are still different: the file_get_contents() function will store the read data into a string, and the readfile() function is stored in the output buffer.

Therefore, the readfile() function can output without the need for output statements such as echo and print. Just "readfile($file);" will do; while the file_get_contents() function requires the use of output statements. Output the read content, "echo file_get_contents($file)".

Let’s take a closer look at the usage of the file_get_contents() function:

file_get_contents($filename,$include_path,$context,$offset,$max_length)The function only A required parameter $filename (the file to be read can be a file name or a file path), but there are 4 omitted parameters: $include_path (default value FALSE ), $context (file handle environment, the value can be NULL), $offset (the starting position of reading), $max_length (the number of bytes read).

Among them, the $offset parameter cannot be used for remote files; if the $max_length parameter is omitted, the entire content of the file will be read by default as in the above example.

Let’s take a look at the $offset and $max_length parameters.

<?php
header("Content-Type: text/html;charset=utf-8");    //设置字符编码
$file = &#39;test.txt&#39;;
$filestr1 = file_get_contents($file,FALSE,NULL,3);
echo $filestr1;
$filestr2 = file_get_contents($file,FALSE,NULL,0,15);
echo "<br>".$filestr2;
?>
Copy after login

Output result:

How to store all data of a file into a string in php

#It can be seen that the $max_length parameter is set to 15, but only 7 is obtained characters, why is this?

That’s because: in the file_get_contents() function, a UTF-8 encoded Chinese character occupies three characters in length. And if the set acquisition length is not enough, the Chinese character that cannot be obtained will be displayed as ? Number.

<?php
header("Content-Type: text/html;charset=utf-8");    //设置字符编码
$file = &#39;test.txt&#39;;
$filestr1 = file_get_contents($file,FALSE,NULL,6);
echo $filestr1;
$filestr2 = file_get_contents($file,FALSE,NULL,0,16);
echo "<br>".$filestr2;
$filestr3 = file_get_contents($file,FALSE,NULL,0,18);
echo "<br>".$filestr3;
?>
Copy after login

Output result:

How to store all data of a file into a string in php

Okay, that’s all. If you want to know anything else, you can click this. → →php video tutorial

Finally, I recommend reading a classic course "PHP String Processing (Jade Girl Heart Sutra Edition)", it's free~ come and learn !

The above is the detailed content of How to store all data of a file into a string in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!