1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| <?php
$str = fread(STDIN, 1000);
$shortopts = ""; $shortopts .= "f:"; $shortopts .= "t:";
$longopts = array( "from:", "to:", "group_by_month:", );
$options = getopt($shortopts, $longopts);
$path = $options['f'] ?? $options['from'] ?? ""; $target = $options['t'] ?? $options['to'] ?? ""; $group_by_month = $options['group_by_month'] ?? true;
$path = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path);; $target = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $target);;
@mkdir($target); if (!$path || !$target || !file_exists($path) || !file_exists($target)) { exit("参数不可用"); }
echo "使用参数 from=$path to=$target " . PHP_EOL;
function read_all($dir, &$res) { if (!is_dir($dir)) return false;
$handle = opendir($dir);
if ($handle) { while (($fl = readdir($handle)) !== false) { $temp = $dir . DIRECTORY_SEPARATOR . $fl; if (strpos($temp, 'RECYCLE') > -1 || strpos($temp, 'System Volume Information') > -1) { continue; } if (is_dir($temp) && $fl != '.' && $fl != '..') { read_all($temp, $res); } else { if ($fl != '.' && $fl != '..') { $res[] = $temp; } } } } }
function rm_empty_dir($path) { if (is_dir($path) && ($handle = opendir($path)) !== false) { while (($file = readdir($handle)) !== false) { if ($file != '.' && $file != '..') { $curfile = $path . '/' . $file; if (is_dir($curfile)) { if (strpos($curfile, 'RECYCLE') > -1 || strpos($curfile, 'System Volume Information') > -1) { continue; } rm_empty_dir($curfile); if (count(scandir($curfile)) == 2) { rmdir($curfile); } } } } closedir($handle); } }
function Directory($dir) { echo "创建文件夹: $dir" . PHP_EOL; return is_dir($dir) or Directory(dirname($dir)) and mkdir($dir, 0777);
}
function sbasename($filename) { return preg_replace('/^.+[\\\\\\/]/', '', $filename); }
$arr = []; read_all($path, $arr);
$info = date("Y-m", filemtime($arr[0])); echo "开始复制" . PHP_EOL;
foreach ($arr as $item) { $fileName = sbasename($item); $mtime = filemtime($item); if($group_by_month){ $dirName = $target . DIRECTORY_SEPARATOR . date('Y-m', $mtime); }else{ $dirName = dirname(str_replace($path, $target, $item)); }
if (!is_dir($dirName)) { Directory($dirName); } $tmpTarget = $dirName . DIRECTORY_SEPARATOR . $fileName; if (!file_exists($tmpTarget)) { copy($item, $tmpTarget); touch($tmpTarget, $mtime); echo "复制: $item => $tmpTarget" . PHP_EOL; } else { echo "跳过: $item" . PHP_EOL; } } echo "结束复制" . PHP_EOL;
|