php visitFile()遍历指定文件夹函数
2015-01-24信息快讯网
php visitFile()遍历指定文件夹函数,需要在php中遍历文件夹功能的代码,可以参考下。
注:visitFile()有少量修改
<?
// 查看指定文件夹的文件
$fileList = array();
function visitFile($path)
{
global $fileList;
$path = str_replace("\\", "/", $path);
$fdir = dir($path);
while (($file = $fdir->read()) !== false)
{
if($file == '.' || $file == '..'){ continue; }
$pathSub = preg_replace("*/{2,}*", "/", $path."/".$file); // 替换多个反斜杠
$fileList[] = is_dir($pathSub) ? $pathSub."/" : $pathSub;
if(is_dir($pathSub)){ visitFile($pathSub); }
}
$fdir->close();
return $fileList;
}
?>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<form method="get">
<?
$path = str_replace("\\", "/", $path);
$path = preg_replace("*/{2,}*", "/", $path);
?>
路径:<input type="text" name="path" id="path" value="<?=$path;?>"/><br>
<li>磁盘根目录 /</li>
<li>网络本地 ./phpMyAdmin</li>
<li>本地磁盘 file://C: 或者 C:</li>
<br>
<input name="action" type="submit" id="action" value="view" />
<input name="action" type="submit" id="action" value="delete" onclick="if(!confirm('是否删除 '+path.value+' 的所有子文件夹和子文件?')) return false;" />
</form>
<?
if(!empty($path)){
$path = preg_replace("*/{2,}*", "/", $path);
$files = visitFile($path);
switch(strtolower($_GET["action"]))
{
case "view":
foreach($files as $key => $value)
{
printf("No.%4d·%s<br>\r\n", $key+1, $value);
}
break;
case "delete":
$faileFiles = array();
foreach(array_reverse($files) as $value)
{
if(!unlink($value))
{
array_push($faileFiles, $value);
}
}
if(!unlink($path)) { array_push($faileFiles, $path); }
if(count($faileFiles) > 0)
{
printf("<br><br>删除失败文件(%d):<p>\r\n", count($faileFiles));
foreach( $faileFiles as $key => $value)
{
printf("No.%4d·%s<br>\r\n", $key+1, $value);
}
}
break;
}
}
?>
php 无法加载mysql的module的时候的配置的解决方案引发的思考
PHP中创建空文件的代码[file_put_contents vs touch]
用phpmailer实现简单openvpn用户认证的实现代码
php中可能用来加密字符串的函数[base64_encode、urlencode、sha1]
PHP CURL模拟登录新浪微博抓取页面内容 基于EaglePHP框架开发
Uncaught exception com_exception with message Failed to create COM object
比file_get_contents稳定的curl_get_contents分享
shopex主机报错误请求解决方案(No such file or directory)
php array_filter除去数组中的空字符元素
Admin generator, filters and I18n
PHP-CGI进程CPU 100% 与 file_get_contents 函数的关系分析
php中使用Curl、socket、file_get_contents三种方法POST提交数据
php中var_export与var_dump的区别分析
php excel类 phpExcel使用方法介绍
php中使用ExcelFileParser处理excel获得数据(可作批量导入到数据库使用)
php简单提示框alert封装函数
为IP查询添加GOOGLE地图功能的代码
Google Voice 短信发送接口PHP开源版(2010.5更新)
PHP中文URL编解码(urlencode()rawurlencode()
《Head First 设计模式》代码之PHP版(面向对象学习)第1/2页
IIS下PHP连接数据库提示mysql undefined function mysql_connect()
Joomla下利用configuration.php存储简单数据
php file_exists 检查文件或目录是否存在的函数
php is_file 判断给定文件名是否为一个正常的文件
PHP中冒号、endif、endwhile、endfor使用介绍
php file_get_contents函数轻松采集html数据
php is_file()和is_dir()用于遍历目录时用法注意事项