-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesUp.php
71 lines (66 loc) · 1.85 KB
/
filesUp.php
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
<?php
function getExtension($fileName)
{
$name = explode('.', $fileName);
if (count($name) > 1) {
return end($name);
} else {
return null;
}
}
function getClearName($fileName)
{
$name = explode('.', $fileName);
if (count($name) > 1) {
unset($name[count($name) - 1]);
}
return implode('.', $name);
}
function safeMove($oldName, $newName)
{
if (file_exists($newName)) {
$path = dirname($newName);
$name = basename($newName);
$ext = getExtension($name);
if ($ext) {
$ext = '.' . $ext;
}
$name = getClearName($name);
$index = 1;
while (file_exists($newName)) {
$newName = $path . DIRECTORY_SEPARATOR . $name . '(' . $index++ . ')' . $ext;
}
}
rename($oldName, $newName);
}
function filesUp($target, $source)
{
$sourceDir = opendir($source);
$file = readdir($sourceDir);
while ($file != false) {
if (substr($file, 0, 1) !== '.') {
$fullFileName = $source . DIRECTORY_SEPARATOR . $file;
if (is_dir($fullFileName)) {
filesUp($target, $source . DIRECTORY_SEPARATOR . $file);
} else {
$newName = $target . DIRECTORY_SEPARATOR . $file;
safeMove($fullFileName, $newName);
}
}
$file = readdir($sourceDir);
}
}
$currentDirName = realpath('./');
if ($currentDirName) {
$currentDir = opendir($currentDirName);
$file = readdir($currentDir);
while ($file !== false) {
if (substr($file, 0, 1) !== '.') {
$fullFileName = $currentDirName . DIRECTORY_SEPARATOR . $file;
if (is_dir($fullFileName)) {
filesUp($currentDirName, $currentDirName . DIRECTORY_SEPARATOR . $file);
}
}
$file = readdir($currentDir);
}
}