PHP一括移動ファイル改名

1744 ワード

public function changeCoverName(){
//$type = ' ';
//$coverPath = './Public/course_cover/kaoyan/';
$type = ' ';
$coverPath = './Public/course_cover/four-six/';//


$targetPath = './Upload/img/course_img/cover/'.md5($type).'/';//

//
$this->recurseCopy($coverPath,$targetPath);

//
$this->changeName($targetPath);

}

//      ,     
public function recurseCopy($src,$dst) { // ,
$dir = opendir($src);
@mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
$this->recurseCopy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}

//    
public function changeName($targetPath){
$d = dir($targetPath);
while (false !== ($entry = $d->read())) {
$newStr = explode('.',$entry);
$newStr[0] = iconv('gbk','utf-8',$newStr[0]);
$newName = md5($newStr[0]);
// echo $newStr[0].'---'.$newName.'<br/>';
rename($targetPath.$entry, $targetPath.$newName.".".$newStr[1]);
}
$d->close();
}