读取指定目录下所有文件夹及文件
后端笔记 2017-09-05 11:32:14

读取指定目录下的文件夹及文件名

PHP Code复制内容到剪贴板
  1. function get_allfiles($path,&$files) {  
  2.     if(is_dir($path)){  
  3.         $dp = dir($path);  
  4.         while ($file = $dp ->read()){  
  5.             if($file !="." && $file !=".."){  
  6.                 get_allfiles($path."/".$file$files);  
  7.             }  
  8.         }  
  9.         $dp ->close();  
  10.     }  
  11.     if(is_file($path)){  
  12.         $files[] =  $path;  
  13.     }  
  14. }  
  15.   
  16. /* 获取指定目录下的所有文件 */  
  17. function get_filenamesbydir($dir){  
  18.     $files =  array();  
  19.     get_allfiles($dir,$files);  
  20.     return $files;  
  21. }  
  22.   
  23. $captions = get_filenamesbydir(ECMS_PATH.'e/extend/sms_send/caption');  
  24.   
  25. //打印所有文件名,包括路径  
  26. foreach ($captions as $value) {  
  27.     echo $value."<br />";  
  28. }  
  29.   
  30. echo '<pre>';  
  31. print_r($captions);  
  32. die;  

 

写入缓存文件: 

PHP Code复制内容到剪贴板
  1. /* 添加说明 */  
  2. if ($_GET['act'] == 'addCaption') {  
  3.   
  4.     $s = '';  
  5.   
  6.     $filter = $_POST;       //要写入的数据  
  7.   
  8.     //7.2排除hash  
  9.     foreach ($filter as $k => $v) {  
  10.         $exp = '/(ehash_|rhash_)/';  
  11.         if (preg_match($exp$k) || $k == 'file') {  
  12.             unset($filter[$k]);  
  13.         }  
  14.     }  
  15.   
  16.     /******************** 
  17.      * 写入内容到文件 
  18.      ********************/  
  19.     $savaPath = ECMS_PATH . 'e/extend/sms_send/caption/';  
  20.     $fileUrl = 'caption/' . $filter['newstime'] . '.php';//要写入文件的文件名(可以是任意文件名),如果文件不存在,将会创建一个  
  21.     if (!file_exists($savaPath)) {  
  22.         if (!mkdir($savaPath, 0777, true)) {  
  23.             printerror2('创建路径失败,请手动建建立路径' . $savaPath$currentUrl . $ecms_hashur['whhref'], '9');  
  24.         }  
  25.     }  
  26.   
  27.     $string='<?php 
  28. if(!defined(\'InEmpireCMS\')){exit();} 
  29. $con='//  引用变量名  
  30.     $string .= var_export($filter, true);  
  31.     $string .= ";" . PHP_EOL;  
  32.     $generate = file_put_contents($fileUrl$string);  
  33.   
  34.     if ($generate) {  
  35.         printerror2('添加成功'$currentUrl . $ecms_hashur['whhref']);  
  36.     } else {  
  37.         printerror2('添加失败,请重试'$currentUrl . $ecms_hashur['whhref'], '9');  
  38.     }  
  39.   
  40. }  

 

 

php读取指定文件内容的五种方式:

-----第一种方法-----fread()--------

PHP Code复制内容到剪贴板
  1. <?php  
  2. $file_path = "test.txt";  
  3. if(file_exists($file_path)){  
  4. $fp = fopen($file_path,"r");  
  5. $str = fread($fp,filesize($file_path));//指定读取大小,这里把整个文件内容读取出来  
  6. echo $str = str_replace("\r\n","<br />",$str);  
  7. }  
  8. ?>  

 

PHP Code复制内容到剪贴板
  1. <?php  
  2. $file_path = "test.txt";  
  3. if(file_exists($file_path)){  
  4. $str = file_get_contents($file_path);//将整个文件内容读入到一个字符串中  
  5. $str = str_replace("\r\n","<br />",$str);  
  6. echo $str;  
  7. }  
  8. ?>  

 

PHP Code复制内容到剪贴板
  1. <?php  
  2. $file_path = "test.txt";  
  3. if(file_exists($file_path)){  
  4. $fp = fopen($file_path,"r");  
  5. $str = "";  
  6. $buffer = 1024;//每次读取 1024 字节  
  7. while(!feof($fp)){//循环读取,直至读取完整个文件  
  8. $str .= fread($fp,$buffer);  
  9. }   
  10. $str = str_replace("\r\n","<br />",$str);  
  11. echo $str;  
  12. }  
  13. ?>  

 

PHP Code复制内容到剪贴板
  1. <?php  
  2. $file_path = "test.txt";  
  3. if(file_exists($file_path)){  
  4. $file_arr = file($file_path);  
  5. for($i=0;$i<count($file_arr);$i++){//逐行读取文件内容  
  6. echo $file_arr[$i]."<br />";  
  7. }  
  8. /* 
  9. foreach($file_arr as $value){ 
  10. echo $value."<br />"; 
  11. }*/  
  12. }  
  13. ?>  

 

PHP Code复制内容到剪贴板
  1. <?php  
  2. $file_path = "test.txt";  
  3. if(file_exists($file_path)){  
  4. $fp = fopen($file_path,"r");  
  5. $str ="";  
  6. while(!feof($fp)){  
  7. $str .= fgets($fp);//逐行读取。如果fgets不写length参数,默认是读取1k。  
  8. }  
  9. $str = str_replace("\r\n","<br />",$str);  
  10. echo $str;  
  11. }  
  12. ?>  

 

 

 

 

本文来自于:http://www.yoyo88.cn/note/backend/145.html

Powered by yoyo苏ICP备15045725号