两个方法,,随机和条件两种模式<?php <br />
/*<br>
* time:2016年12月2日17:58:36<br>
* author:张晨阳<br>
* email:lequne@126.com<br>
*/<br>
//根据判断条件生成不同的文件名<br>
function fRename($dirname)<br>
{<br>
if (!is_dir($dirname)) {<br>
echo "{$dirname}不是一个有效的目录!";<br>
exit();<br>
}<br>
$handle = opendir($dirname);<br>
<br>
while (($fn = readdir($handle)) !== false) {<br>
if ($fn != '.' && $fn != '..') {<br>
<br>
echo "fn = " . $fn . "\n\r";<br>
$curDir = $dirname . '/' . $fn;<br>
<br>
echo "curDir = " . $curDir . "\r\n";<br>
if (is_dir($curDir)) {<br>
fRename($curDir);<br>
} else {<br>
<br>
$path = pathinfo($curDir);<br>
//var_dump($path);//die;<br>
//$newname = $path['dirname'].'/'.rand(0,100).'.'.$path['extension'];<br>
//得到图片的信息<br>
$img_info = getimagesize($curDir);<br>
<br>
$w = $img_info[0];<br>
$h = $img_info[1];<br>
// var_dump($w);<br>
if ($w == 210 && $h == 210) {<br>
$newname = '1.jpg';<br>
} elseif ($w == 720 && $h == 268) {<br>
$newname = '2.jpg';<br>
} elseif ($w == 298 && $h == 198) {<br>
$newname = '3.jpg';<br>
} elseif ($w == 338 && $h == 365) {<br>
$newname = '4.jpg';<br>
} elseif ($w == 425 && $h == 425) {<br>
$newname = '5.jpg';<br>
} else {<br>
$newname = $fn;<br>
}<br>
//$new=rand(10,100000000000);<br>
//$n=$new.'.jpg';<br>
//$newname1='.jpg';<br>
var_dump($curDir);<br>
$newname = str_replace($fn, $newname, $curDir);<br>
var_dump($newname);<br>
// die;<br>
echo "替换之后的newname:" . $newname . "\r\n";<br>
rename($curDir, $newname);<br>
// echo $curDir.'---'.$newname."<br>";<br>
}<br>
<br>
}<br>
<br>
}<br>
}<br>
<br>
//调用该方法<br>
<br>
//fRename('./001');<br>
<br>
<br>
//利用PHP目录和文件函数遍历用户给出目录的所有的文件和文件夹,修改文件名称随机<br>
function rand_fRename($dirname)<br>
{<br>
header('content-type:text/html;charset=utf8');<br>
if (!is_dir($dirname)) {<br>
echo "{$dirname}不是一个有效的目录!";<br>
exit();<br>
}<br>
$handle = opendir($dirname);<br>
<br>
while (($fn = readdir($handle)) !== false) {<br>
if ($fn != '.' && $fn != '..') {<br>
<br>
echo "fn = " . $fn . "\n\r";<br>
$curDir = $dirname . '/' . $fn;<br>
<br>
echo "curDir = " . $curDir . "\r\n";<br>
if (is_dir($curDir)) {<br>
$new = generate_password();<br>
<br>
// var_dump($new);die;<br>
<br>
$newname = str_replace($fn, $new, $curDir);<br>
var_dump($newname);<br>
rename($curDir, $newname);<br>
rand_fRename($newname);<br>
}<br>
else{<br>
<br>
$path = pathinfo($curDir);<br>
//var_dump($path);//die;<br>
//$newname = $path['dirname'].'/'.rand(0,100).'.'.$path['extension'];<br>
//123[1] 123<br>
//得到图片的信息<br>
$img_info=getimagesize($curDir);<br>
// var_dump($img_info);<br>
<br>
$w=$img_info[0];<br>
$h=$img_info[1];<br>
<br>
//防止出现重复随机生成文件名字<br>
$new=rand(10,100000000000);<br>
$n=$new.'.jpg';<br>
//$newname1='.jpg';<br>
var_dump($curDir);<br>
$newname = str_replace($fn,$n,$curDir);<br>
var_dump($newname);<br>
// die;<br>
echo "替换之后的newname:".$newname."\r\n";<br>
rename($curDir,$newname);<br>
<br>
}<br>
<br>
}<br>
<br>
}<br>
}<br>
<br>
rand_fRename(); //需要更改的文件夹文件夹<br>
<br>
//随机生成字符串<br>
function generate_password($length = 8)<br>
{<br>
header('content-type:text/html;charset=utf8');<br>
// 密码字符集,可任意添加你需要的字符 <br>
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';<br>
$password = "";<br>
for ($i = 0; $i
// 这里提供两种字符获取方式 <br>
// 第一种是使用 substr 截取$chars中的任意一位字符; <br>
// 第二种是取字符数组 $chars 的任意元素 <br>
// $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1); <br>
$password .= $chars[mt_rand(0, strlen($chars) - 1)];<br>
}<br>
return $password;<br>
}