The most commonly used methods for reading local files in php are fopen and fread functions. There are other functions such as php file_get_contents that can read local files or remote files. Let's introduce them one by one below.
fopen() function
Open the file directly
Example
The code is as follows |
Copy code |
代码如下 |
复制代码 |
$file = "111.txt";
$fp = fopen($file,"r");
if ($fp){
while(!feof($fp)){
//第二个参数为读取的长度
$data = fread($fp, 1000);
}
fclose($fp);
}
echo $data;
?>
|
$file = "111.txt";
$fp = fopen($file,"r");
if ($fp){
while(!feof($fp)){
//The second parameter is the read length
代码如下 |
复制代码 |
echo file_get_contents("test.txt");
?>
输出:
This is a test file with test text.
|
$data = fread($fp, 1000);
}
fclose($fp);
代码如下 |
复制代码 |
$dir = opendir('/movie');
while(($file = readdir($dir))!=false){
if ($file!="." && $file!="..") {
$ns = explode('.', $file);
echo $ns[0];
}
}
closedir($dir);
?>
|
}
echo $data;
?>
|
The file_get_contents() function reads the entire file into a string
Example
The code is as follows |
Copy code |
echo file_get_contents("test.txt");
?>
Output:
This is a test file with test text.
|
php reads local folder files
The code is as follows |
Copy code |
$dir = opendir('/movie');
while(($file = readdir($dir))!=false){
if ($file!="." && $file!="..") {
echo $ns[0];
}
}
closedir($dir);
?>
|
http://www.bkjia.com/PHPjc/445626.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445626.htmlTechArticleThe most common way to read local files in php is to configure and use the fopen and fread functions. There are also Some others like php file_get_contents can read local files or remote files...