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();
}