批量更改文件名称--PHP脚本
程序员文章站
2022-04-18 17:16:15
...
header("Content-Type:text/html;charset=utf-8");
set_time_limit(0);
//搜索目录文件名称
$dir = "./qrcode/";
if(is_dir($dir)) {
$file = opendir($dir);
while(($name = readdir($file)) != false) {
if ($name != '.' && $name != '..') {
$dir_list[] = $name;
}
}
}
closedir($file);
echo "目录的文件个数:".count($dir_list);
echo "<br>";
//搜索数据库数据
$conn = @mysql_connect("localhost","root","mysql") or die ("Connect MySQL False");
@mysql_select_db("oa",$conn);
@mysql_query("SET NAMES UTF8");
$sql = "select id,title,guide,qrcode from oa_collection";
$rs = @mysql_query($sql);
while($rst = @mysql_fetch_row($rs)){
$mysql_list[] = $rst;
}
echo "数据库搜索的个数:".count($mysql_list);
echo "<br>";
//处理文件
$i = 0;
$j = 0;
foreach($dir_list as $key=>$val) {
foreach($mysql_list as $k=>$v) {
$i++;
if($val==basename($v[3])) {
$j++;
$new_name = $v[0]."_".$v[2]." ".$v[1].".png";
$is_true = rename($dir.$val,iconv('UTF-8','GBK',$dir.$new_name));
if($is_true) {
echo " ".$val."执行结果是".$is_true;
} else {
echo " ".$val."执行结果是".$is_true;
}
}
// ob_flush();
// flush();
}
}
echo "一共执行的循环数:".$i;
echo "<br>";
echo "重命名执行的次数:".$j;