您的位置:首页 > 编程语言 > PHP开发

php读取本地文件操作函数

2013-09-05 18:33 344 查看
在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);
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: