当前位置:Gxlcms > PHP教程 > php读取本地文件操作函数_PHP教程

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

时间:2021-07-01 10:21:17 帮助过:2人阅读

在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可以读本地文件也可以读远程文件...

人气教程排行