Heim  >  Artikel  >  Backend-Entwicklung  >  php读取本地文件操作函数_PHP教程

php读取本地文件操作函数_PHP教程

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

在php中读取本地文件我们最常用的就是fopen与fread函数配置使用即可了,还有一些其它的像php file_get_contents可以读本地文件也可以读远程文件了,下面我们来一一介绍一下。  

fopen() 函数
直接打开文件

 代码如下 复制代码

$file = "111.txt";
$fp = fopen($file,"r");
if ($fp){
 while(!feof($fp)){
  //第二个参数为读取的长度
  $data = fread($fp, 1000);
 }
 fclose($fp);
}
echo $data;
?>

file_get_contents() 函数把整个文件读入一个字符串中

例子

 代码如下 复制代码

echo file_get_contents("test.txt");
?>

输出:

This is a test file with test text.

php读取本地文件夹文件

 代码如下 复制代码

$dir = opendir('/movie');
while(($file = readdir($dir))!=false){
  if ($file!="." && $file!="..") {
    $ns = explode('.', $file);
    echo $ns[0];
  }
}
closedir($dir);

?>


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445626.htmlTechArticle在php中读取本地文件我们最常用的就是fopen与fread函数配置使用即可了,还有一些其它的像php file_get_contents可以读本地文件也可以读远程文件...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn