//列出目錄下文件
function file_list($path){$num = 0;
if($handle = opendir($path)){
while(false !== $file=readdir($handle)){
if($file !='.' && $file !='..'){
if(is_dir($path. '/'. $file)){
file_list($path. '/'. $file);
}else{
if(preg_match ("/.php$/", $file)){ //這裏匹配是否PHP文件
file_content_replace($path.'/'.$file, 'PPCART_TEXT_SUCCESS', '***********'); //這裏的替換方式;開始替換文本
echo '++++++++++'. $file.'++++++++++<br />';
}
else echo '------非PHP文件------<br />';
$num++;
}
}
}
closedir($handle);
}
}
function myScanDir($path) {
$result = scandir($path);
foreach($result as $re) {
if($re == '.' || $re == '..') continue;
$dir = $path.'/'.$re;
if(is_dir($dir)) {
showDir($dir);
continue;
}
echo $dir."<br />";
}
}
//比較替換;參數依次:文件名、被替換字符串、替換字符串;file_get_contents(),file_put_contents(),str_replace()配合使用
function file_content_replace($filename, $search, $replace){
$string = file_get_contents($filename);
$new_string = str_replace($search, $replace, $string);
if($string !=$new_string) file_put_contents($filename, $new_string);
}
//////////////////////===================
操作文件的函數有很多有用的:
===========================
file()把文件讀入一個數組中,(便可以逐行顯示文件內容)
//逐行顯示文件內容
function show_line($file){
$lines = file($file);
foreach($lines as $line_num => $line){
if(preg_match ("/PPCART_TEXT_SUCCESS/", $line)){
$add = '********************math********************';
}else{
$add = '-----------------------------------------------';
}
echo "Line #<b>{$line_num}</b>: ". htmlspecialchars($line). $add. '<br />';
}
$line = NUll;
$lines = NUll;
$line_num = NUll;
}
===========================
不把文件內在讀出來放到另人變量裏的,直接讀取文件緩存中的內容
$fp = fopen($file, 'r'); //以讀方式打開文件;$fp = fopen($file, 'r+');//以讀寫方式打開;$fp = fopen($file, 'a');//以寫追加方式打開
// read some data
$data = fread($fp, 18);
//fgetc()讀字符串 | fgets()讀行 | fgetss()讀行並去標籤 | fread()讀文件 | stream_get_line()讀文件
var_dump($data);echo '-----------<br />';
// move back to the begining of the file
// same as rewind($fp);
fseek($fp, 10); //設置文件指針
$data = fread($fp, 9);
var_dump($data);echo '<br />';
fclose($fp);