Browse Source

add dir copy function

elviss 2 years ago
parent
commit
5f4239d786
2 changed files with 49 additions and 4 deletions
  1. 1 1
      src/PackageClass.php
  2. 48 3
      src/Utility.php

+ 1 - 1
src/PackageClass.php

@@ -356,7 +356,7 @@ class PackageClass
 				logInfo("skip copy file, file already exists",$targetPath);
 			}
 			$targetPath = pathJoin($rootDir,$targetName);
-			if( fileCopy($filePath,$targetPath) ){
+			if( filedirCopy($filePath,$targetPath) ){
 				logInfo("succeeded to copy file",$fileName," -> ",$targetName);
 			}
 			else{

+ 48 - 3
src/Utility.php

@@ -84,8 +84,8 @@ function pathJoin($base, $path) {
     return $newPath;
 }
 
-//拷贝文件,自动创建目录,自动覆盖
-function fileCopy(string $filePath, string $targetPath){
+//拷贝文件,自动创建目录,自动覆盖
+function fileCopy(string $sourcePath, string $targetPath){
 	$targetDir = dirname($targetPath);
 	if(!is_dir($targetDir)){
 		createDirectory($targetDir);
@@ -94,10 +94,55 @@ function fileCopy(string $filePath, string $targetPath){
 	if( file_exists($targetPath) ){
 		unlink($targetPath);
 	}
-	copy($filePath,$targetPath);
+	copy($sourcePath,$targetPath);
 	return file_exists($targetPath);
 }
 
+//拷贝目录,自动创建目录,自动覆盖
+function dirCopy(string $sourceDir, string $targetDir){
+	if(! is_dir($targetDir)){
+		createDirectory($targetDir);
+	}
+	$dir = opendir($sourceDir); 
+	while(false !== ( $file = readdir($dir)) ) { 
+		if($file == '.' ){
+			continue;
+		}
+		if($file == '..' ){
+			continue;
+		}
+		$filePath = $sourceDir . '/' . $file;
+		$targetPath = $targetDir . '/' . $file;
+		if ( is_dir($filePath) ) { 
+			if(! dirCopy($filePath,$targetPath) ){
+				logError('failed to copy dir',$filePath,'->',$targetPath);
+				return false;
+			}
+		} 
+		else { 
+			copy($filePath,$targetPath); 
+			if(! file_exists($targetPath) ){
+				logError('failed to copy file',$filePath,'->',$targetPath);
+				return false;
+			}
+		}
+	} 
+	closedir($dir); 
+	return true;
+}
+//拷贝文件或目录
+function filedirCopy(string $sourcePath, string $targetPath){
+	if(! file_exists($sourcePath)){
+		logError('failed to copy file',$sourcePath,'->',$targetPath);
+		return false;
+	}
+	if( is_dir($sourcePath)){
+		return dirCopy($sourcePath,$targetPath);
+	}else{
+		return fileCopy($sourcePath,$targetPath);
+	}
+}
+
 //枚举子目录
 function scanSubDir($dir,$exclude_names = array()){
 	$filelist = scandir($dir);