php中,可以用用DirectoryIterator获取指定目录的文件或者目录.
$path = "/tmp"; $oDir = new DirectoryIterator($path); foreach($oDir as $file) { if($file->isfile()) { $tmpFile['link'] = $file->getPath(); $tmpFile['name'] = $file->getFileName(); $tmpFile['type'] = 'file'; $tmpFile['size'] = _cal_size($file->getSize()); $tmpFile['mtime'] = $file->getMTime(); $arrFile[] = $tmpFile; } } print_r($arrFile); /* output Array ( [0] => Array ( [link] => /tmp [name] => scim-panel-socket-:0-root [type] => dir [size] => 0b [mtime] => 1222049895 ) [1] => Array ( [link] => /tmp [name] => .font-unix [type] => dir [size] => 4k [mtime] => 1241417372 ) ) */
RecursiveDirectoryIterator 获取目录下所有的文件,包括子目录 ,其中搭配:
RecursiveIteratorIterator使用.
(RecursiveIteratorIterator是个递归迭代器,其后可选带四个参数(只能任一)
RecursiveIteratorIterator::LEAVES_ONLY
默认,已在__construct中设定使用
作用是去枝留叶,跳过空节点,只递归取实值
举例就是
1.递归文件夹取文件时跳过文件夹本身,只取文件夹下面的文件,输出的项全部是file(文件和各级子文件夹的文件)
2.多维数组就跳过前几维的key,而取value,输出的每一项都不是array
3.XML只取值(text),不输出节点名,当然还要视乎你设定获取xml什么内容
RecursiveIteratorIterator::SELF_FIRST
各项都包含,例如递归文件夹就会连同子文件夹名称也作为其中项输出,顺序是先父后子
RecursiveIteratorIterator::CHILD_FIRST
同上,但顺序是先子后父,./test/test.php会在./test(文件夹)前面)
$path = "/tmp/";
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
foreach($objects as $object)
{
$tmpFile['link'] = $object->getPath();
$tmpFile['name'] = $object->getFileName();
$tmpFile['type'] = $object->isFile() ? 'file' : 'dir';
$tmpFile['size'] = _cal_size($object->getSize());
$tmpFile['mtime'] = $object->getMTime();
$arrFile[] = $tmpFile;
}
print_r($arrFile);
/*
output:
Array
(
[0] => Array
(
[link] => /tmp
[name] => scim-panel-socket-:0-root
[type] => dir
[size] => 0b
[mtime] => 1222049895
)
[1] => Array
(
[link] => /tmp/.font-unix
[name] => fs7100
[type] => dir
[size] => 0b
[mtime] => 1241417372
)
)
*/
再来看个例子:
/*** the target directory, no trailling slash ***/
$directory = './';
try
{
/*** check if we have a valid directory ***/
if( !is_dir($directory) )
{
throw new Exception('Directory does not exist!'."\n");
}
/*** check if we have permission to rename the files ***/
if( !is_writable( $directory ))
{
throw new Exception('You do not have renaming permissions!'."\n");
}
/**
*
* @collapse white space
*
* @param string $string
*
* @return string
*
*/
function collapseWhiteSpace($string)
{
return preg_replace('/\s+/', ' ', $string);
}
/**
* @convert file names to nice names
*
* @param string $filename
*
* @return string
*
*/
function safe_names($filename)
{
$filename = collapseWhiteSpace($filename);
$filename = str_replace(' ', '-', $filename);
$filename = preg_replace('/[^a-z0-9-.]/i','',$filename);
return strtolower($filename);
}
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, 0));
/*** loop directly over the object ***/
while($it->valid())
{
/*** check if value is a directory ***/
if(!$it->isDot())
{
if(!is_writable($directory.'//m.sbmmt.com/m/'.$it->getSubPathName()))
{
echo 'Permission Denied: '.$directory.'//m.sbmmt.com/m/'.$it->getSubPathName()."\n";
}
else
{
/*** the old file name ***/
$old_file = $directory.'//m.sbmmt.com/m/'.$it->getSubPathName();
/*** the new file name ***/
$new_file = $directory.'//m.sbmmt.com/m/'.$it->getSubPath().'//m.sbmmt.com/m/'.safe_names($it->current());
/*** rename the file ***/
rename ($old_file, $new_file);
/*** a little message to say file is converted ***/
echo 'Renamed '. $directory.'//m.sbmmt.com/m/'.$it->getSubPathName() ."\n";
}
}
/*** move to the next iteration ***/
$it->next();
}
/*** when we are all done let the user know ***/
echo 'Renaming of files complete'."\n";
}
catch(Exception $e)
{
echo $e->getMessage()."\n";
}
?>
相关文章推荐
• 分享PHP函数使用小工具(附代码示例)• PHP安全编码总结(经验分享)• 非常全面!PHP常见漏洞代码总结!• 一文详解PHP实现职责链设计模式(附代码示例)• php实现通过JSON RPC与go通讯(附代码)独孤九贱(3)_JavaScript视频教程
javascript是运行在浏览器上的脚本语言,连续多年,被评为全球最受欢迎的编程语言。是前端开发必备三大法器中,最具杀伤力。如果前端开发是降龙十八掌,好么javascript就是第18掌:亢龙有悔。没有它,你的前端生涯是不完整的。《php.cn独孤九贱(3)-JavaScript视频教程》课程特色:php中文网原创幽默段子系列课程,以恶搞,段子为主题风格的php视频教程!轻松的教学风格,简短的教学模式,让同学们在不知不觉中,学会了javascript知识。
JavaScript教程141785次播放
独孤九贱(6)_jQuery视频教程
jQuery是一个快速、简洁的JavaScript框架。设计的宗旨是“write Less,Do More”,即倡导写更少的代码,做更多的事情。它封装JavaScript常用的功能代码,提供一种简便的JavaScript设计模式,优化HTML文档操作、事件处理、动画设计和Ajax交互。 核心特性可以总结为:具有独特的链式语法和短小清晰的多功能接口;具有高效灵活的css选择器,并且可对CSS选择器进行扩展;拥有便捷的插件扩展机制和丰富的插件。兼容各种主流浏览器,如IE 6.0+、FF 1.5+、Safari 2.0+、Opera 9.0+等,是全球最流行的前端开发框架之一。PHP中文网根据最新版本,独家录制jQuery最新视频教程,回馈PHP中文网的新老用户。
jQuery教程116401次播放
jQuery与Ajax基础与实战
jQuery是最流行的JS函数库,封装了许多实用的功能,其中最引人入胜的就是Ajax。 jQuery中的Ajax操作,语法简单,操作方便,使Ajax从未如此轻松,前端人员从此不再为与服务器异步交互而发愁,本套课程,精选了最常用的几个方法,从基本的语法到每个参数,再到具体实例进行了全面的讲解。
AJAX教程16075次播放
Git教程(60分钟全程无废话版)
Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。 Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。 Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持
JavaScript教程13958次播放