Home  >  Article  >  Backend Development  >  PHP reads local file operation function_PHP tutorial

PHP reads local file operation function_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 10:58:591104browse

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 stringExample
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!="..") {
$ns = explode('.', $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...
Statement:
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