在php中讀取本地文件我們最常用的就是fopen與fread函數(shù)配置使用即可了,還有一些其它的像php file_get_contents可以讀本地文件也可以讀遠(yuǎn)程文件了,下面我們來一一介紹一下.
fopen() 函數(shù),直接打開文件,例代碼如下:
- <?php
- $file = "111.txt";
- $fp = fopen($file,"r");
- if ($fp){
- while(!feof($fp)){
- //第二個參數(shù)為讀取的長度
- $data = fread($fp, 1000);
- }
- fclose($fp);
- }
- echo $data;
- ?>
file_get_contents() 函數(shù)把整個文件讀入一個字符串中,例子代碼如下:
- <?php
- echo file_get_contents("test.txt");
- ?>
- //輸出:
- This is a test file with test text.
php讀取本地文件夾文件,代碼如下:
- <?php
- $dir = opendir('/movie');
- while(($file = readdir($dir))!=false){
- if ($file!="." && $file!="..") {
- $ns = explode('.', $file);
- echo $ns[0];//開源代碼Vevb.com
- }
- }
- closedir($dir);
- ?>
|
新聞熱點(diǎn)
疑難解答