使用PHP遍歷文件目錄與清除目錄中文件的實(shí)現(xiàn)詳解
更新時(shí)間:2013年06月24日 09:24:01 作者:
本篇文章是對(duì)使用PHP遍歷文件目錄與清除目錄中文件的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
今天無(wú)聊中練習(xí)了一下PHP遍歷文件目錄的程序,編寫了以下兩個(gè)程序,不過(guò)質(zhì)量不是很好,輕拍~~~
1、清除PHP緩存文件
<?php
function read_dir($dir,$file)
{
$a =strpos($file,".php");
if($a>0)
{
unlink($dir . $file);
echo "delete $dir$file <br>";
return true;
}
if(strpos($file,".") === 0 || strpos($file,".") !== false ) return true;
if(strpos($file,".") === false || strpos($dir,"/") === false)
{
$dir = $dir . $file . "/";
if(!is_dir($dir)) return false;
$dh = opendir($dir);
while(($file = readdir($dh)) != false)
{
read_dir($dir,$file); //遞歸調(diào)用
}
}
}
function clear_caches()
{
$dir = "./temp/"; //要清除的PHP緩存文件目錄
if(!is_dir($dir)) die("It is not a dir");
$dh = opendir($dir);
while(($file = readdir($dh) )!=false)
{
//var_dump($file);
read_dir($dir,$file);
}
}
?>
2、遍歷目錄中所有文件
<html>
<head>
<meta http-enquiv="Content-Type" content="text/html;charset=gb2312">
<title>查看目錄</title>
</head>
<body>
<table width="600" align="center">
<tr>
<th width="50%">文件名</th>
<th width="25%">修改時(shí)間</th>
<th width="25%">文件大?。╧)</th>
</tr>
<?php
//$dir = "./admin/";
$dir = "c:/";
$up_dir = "上級(jí)目錄";
$up_url = $dir;
if(isset($_REQUEST['act']) && $_REQUEST['act']=='list_dir')
{
if(emptyempty($_REQUEST['dir']))
{
$up_dir="目錄為空!";
}
$dir = isset($_REQUEST['dir']) ? $_REQUEST['dir'] : $dir;
}
if(!is_dir($dir))
{
$up_dir="無(wú)效目錄!";
}
?>
<tr>
<td colspan="3">
<?php
if(strpos($up_dir,"上級(jí)目錄")!==false)
{
//if($up_url=="") echo $up_dir;
if($dir != "./admin/")
{
$up_url = substr($dir,0,-1);
$k = strrpos($up_url,"/");
$up_url = substr($up_url,0,$k-strlen($up_url));
$up_url = $up_url ."/";
}
echo "<a href=\"test.php?act=list_dir&dir=$up_url\">$up_dir</a>";
}
else
{
echo $up_dir;
die();
}
?>
</td>
</tr>
<?php
$up_dir = $dir;
$dh = opendir($dir);
while(($file=readdir($dh)) != false)
{
if($file != "." && $file != ".." && $file != ".svn" )
{
if(strpos($file,".") !==false)
{
$time = date("Y-m-d H:i:s", filectime($dir . $file));
$size = filesize($dir . $file)/1000;
echo "<tr><td>$file</td><td>$time</td><td>$size</td></tr>";
}
else
{
$time = date("Y-m-d H:i:s.", filectime($dir . $file));
$size = filesize($dir . $file)/1000;
$dir = $dir . $file ."/";
echo "<tr><td><a href =\"test.php?act=list_dir&dir=$dir\">$file</a></td><td>$time</td><td>$size</td></tr>";
$dir = $up_dir;
}
}
}
?>
</table>
<?php die(); ?>
</body>
</html>
1、清除PHP緩存文件
復(fù)制代碼 代碼如下:
<?php
function read_dir($dir,$file)
{
$a =strpos($file,".php");
if($a>0)
{
unlink($dir . $file);
echo "delete $dir$file <br>";
return true;
}
if(strpos($file,".") === 0 || strpos($file,".") !== false ) return true;
if(strpos($file,".") === false || strpos($dir,"/") === false)
{
$dir = $dir . $file . "/";
if(!is_dir($dir)) return false;
$dh = opendir($dir);
while(($file = readdir($dh)) != false)
{
read_dir($dir,$file); //遞歸調(diào)用
}
}
}
function clear_caches()
{
$dir = "./temp/"; //要清除的PHP緩存文件目錄
if(!is_dir($dir)) die("It is not a dir");
$dh = opendir($dir);
while(($file = readdir($dh) )!=false)
{
//var_dump($file);
read_dir($dir,$file);
}
}
?>
2、遍歷目錄中所有文件
復(fù)制代碼 代碼如下:
<html>
<head>
<meta http-enquiv="Content-Type" content="text/html;charset=gb2312">
<title>查看目錄</title>
</head>
<body>
<table width="600" align="center">
<tr>
<th width="50%">文件名</th>
<th width="25%">修改時(shí)間</th>
<th width="25%">文件大?。╧)</th>
</tr>
<?php
//$dir = "./admin/";
$dir = "c:/";
$up_dir = "上級(jí)目錄";
$up_url = $dir;
if(isset($_REQUEST['act']) && $_REQUEST['act']=='list_dir')
{
if(emptyempty($_REQUEST['dir']))
{
$up_dir="目錄為空!";
}
$dir = isset($_REQUEST['dir']) ? $_REQUEST['dir'] : $dir;
}
if(!is_dir($dir))
{
$up_dir="無(wú)效目錄!";
}
?>
<tr>
<td colspan="3">
<?php
if(strpos($up_dir,"上級(jí)目錄")!==false)
{
//if($up_url=="") echo $up_dir;
if($dir != "./admin/")
{
$up_url = substr($dir,0,-1);
$k = strrpos($up_url,"/");
$up_url = substr($up_url,0,$k-strlen($up_url));
$up_url = $up_url ."/";
}
echo "<a href=\"test.php?act=list_dir&dir=$up_url\">$up_dir</a>";
}
else
{
echo $up_dir;
die();
}
?>
</td>
</tr>
<?php
$up_dir = $dir;
$dh = opendir($dir);
while(($file=readdir($dh)) != false)
{
if($file != "." && $file != ".." && $file != ".svn" )
{
if(strpos($file,".") !==false)
{
$time = date("Y-m-d H:i:s", filectime($dir . $file));
$size = filesize($dir . $file)/1000;
echo "<tr><td>$file</td><td>$time</td><td>$size</td></tr>";
}
else
{
$time = date("Y-m-d H:i:s.", filectime($dir . $file));
$size = filesize($dir . $file)/1000;
$dir = $dir . $file ."/";
echo "<tr><td><a href =\"test.php?act=list_dir&dir=$dir\">$file</a></td><td>$time</td><td>$size</td></tr>";
$dir = $up_dir;
}
}
}
?>
</table>
<?php die(); ?>
</body>
</html>
您可能感興趣的文章:
- php遍歷目錄與文件夾的多種方法詳解
- PHP遍歷某個(gè)目錄下的所有文件和子文件夾的實(shí)現(xiàn)代碼
- php 遍歷顯示文件夾下所有目錄、所有文件的函數(shù),沒(méi)有分頁(yè)的代碼
- PHP 遠(yuǎn)程文件管理,可以給表格排序,遍歷目錄,時(shí)間排序
- PHP使用glob函數(shù)遍歷目錄或文件夾的方法
- PHP遞歸遍歷指定目錄的文件并統(tǒng)計(jì)文件數(shù)量的方法
- PHP遍歷并打印指定目錄下所有文件實(shí)例
- php實(shí)現(xiàn)遍歷目錄并刪除指定文件中指定內(nèi)容
- PHP不用遞歸遍歷目錄下所有文件的代碼
- PHP采用自定義函數(shù)實(shí)現(xiàn)遍歷目錄下所有文件的方法
- php遍歷刪除整個(gè)目錄及文件的方法
- php遍歷替換目錄下文件指定內(nèi)容的方法
- PHP遍歷目錄文件的常用方法小結(jié)
相關(guān)文章
php實(shí)現(xiàn)跨域提交form表單的方法【2種方法】
這篇文章主要介紹了php實(shí)現(xiàn)跨域提交form表單的方法,結(jié)合實(shí)例形式分析了curl及ajax兩種方法進(jìn)行跨域提交的操作技巧,需要的朋友可以參考下2016-10-10
php實(shí)現(xiàn)概率性隨機(jī)抽獎(jiǎng)代碼
本文給大家分享的是使用php根據(jù)獎(jiǎng)品的權(quán)重來(lái)實(shí)現(xiàn)概率性隨機(jī)抽獎(jiǎng)的代碼,非常的使用,有類似需求的小伙伴,可以拿去參考下2016-01-01
關(guān)于PHP模板Smarty的初級(jí)使用方法以及心得分享
本篇文章是對(duì)PHP模板Smarty的初級(jí)使用方法以及心得進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
淺析PHP中json_encode與json_decode的區(qū)別
這篇文章主要介紹了淺析PHP中json_encode與json_decode的區(qū)別,PHP提供json_encode()和json_decode()函數(shù),前者用于編碼,后者用于解碼。下面來(lái)分析下這兩個(gè)函數(shù),感興趣的可以了解一下2020-07-07

