兩種方法,,隨機和條件兩種模式<?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 . "nr";<br>
$curDir = $dirname . '/' . $fn;<br>
<br>
echo "curDir = " . $curDir . "rn";<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 . "rn";<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 . "nr";<br>
$curDir = $dirname . '/' . $fn;<br>
<br>
echo "curDir = " . $curDir . "rn";<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 //$newname = $path['dirname'].'/'.rand(0,100).'.'.$path['extension'];<br>);<br>
//123[1] 123🎜 //有圖片的資料<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."rn";<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>
}