<?php
header('Content-Type: text/html; charset=gb2312');
function searchFiles( $srcPath = '', $dstFile = '*', $subDir = false ){
static $files = array();
if ( !empty( $srcPath ) ) $srcPath = rtrim( $srcPath, '/' ).'/';
$file = glob( $srcPath.$dstFile );
if ( !empty( $file ) ) $files = array_merge( $files, $file );
if ( $subDir ){
foreach( glob( $srcPath.'*', GLOB_ONLYDIR ) as $dir ){
searchFiles( $dir, $dstFile, $subDir );
}
}
return $files;
}
$files = searchFiles( './', '*.txt', true );
foreach( $files as $file ){
if ( !is_writable( $file ) ) continue;
$newname = preg_replace( '/(_\d*)*\.txt+$/i', '.txt', $file );
if ( is_file( $newname ) ) unlink($file);//如果已经存在重命名后的文件,立即删除。
rename( $file, $newname );//执行重命名
}
?>
请问高手这样写对吗?
header('Content-Type: text/html; charset=gb2312');
function searchFiles( $srcPath = '', $dstFile = '*', $subDir = false ){
static $files = array();
if ( !empty( $srcPath ) ) $srcPath = rtrim( $srcPath, '/' ).'/';
$file = glob( $srcPath.$dstFile );
if ( !empty( $file ) ) $files = array_merge( $files, $file );
if ( $subDir ){
foreach( glob( $srcPath.'*', GLOB_ONLYDIR ) as $dir ){
searchFiles( $dir, $dstFile, $subDir );
}
}
return $files;
}
$files = searchFiles( './', '*.txt', true );
foreach( $files as $file ){
if ( !is_writable( $file ) ) continue;
$newname = preg_replace( '/(_\d*)*\.txt+$/i', '.txt', $file );
if ( is_file( $newname ) ) unlink($file);//如果已经存在重命名后的文件,立即删除。
rename( $file, $newname );//执行重命名
}
?>
请问高手这样写对吗?
解决方案 »
- php有没有什么方法可以从本地的一个端口读取文件啊?
- 小问题来了。
- 求教web service
- 高分:推荐一个成熟的英文分词代码,根据词典分出各种短语
- 求php和mysql web开发第四版的代码
- 为什么我这一段程序在当前页面可以得到值。第二个页面就不行?
- php调用c#soap---no operations defined in the WSDL document!
- 用thinkphp写的会员登录总是报错,求解决
- php报错,搭建egroupware,unexpected T_ARRAY, expecting '&' or T_VARIABLE
- 支付宝异步通知问题
- 做一个网站,从哪里入手
- email.class.php 发邮件类,发送密送邮件
首先建立一个2维数组,用来分别存放文件夹名和文件名
1,假如有a,b,c三个文件夹,则首先打开a文件夹,循环把该文件夹内的所有文件名及文件夹名存放到数组内
2,打开文件夹b,用同上边一样的方法把文件夹b内所有的文件名以及文件夹名存放到数组内
3,同样把c文件夹下的文件名以及文件夹名都放入同一个数组内
4,找出数组内的重复值,并将重复的值写入新数组内,
5,循环该新数组,根据文件夹名和文件名删除文件即可.
把要处理的目录放在第一,就是$array13.删掉交集结果的文件,从数组unset相关文件名4.循环把$array2替换为其他目录的数组,重复2-3
这里适当使用一下array_merge或其他数组函数可以省一些循环5.要处理其他目录的话就替换$array1,循环重复2-4步骤
还是那句,善用一下数组合并、交并差比较的函数,减少循环如果要比较内容(md5_file),适当在上面加入目录很多的话,建议看看spl