-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffret_ajax.php
102 lines (89 loc) · 2.46 KB
/
ffret_ajax.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
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
<?php
$errorMSG = array();
if (empty($_POST["fullpath"])) {
$errorMSG = array('pathErr' => "Path is required");
} else {
$errorMSG = array();
$fullpath = $_POST["fullpath"];
if(!file_exists($fullpath) && !is_dir($fullpath)) {
$errorMSG = array('pathErr' => "File not exists");
}
}
if(!empty($errorMSG)) {
echo json_encode(['status'=>false, 'msg'=>$errorMSG]);
}
if(empty($errorMSG)) {
if(is_dir($fullpath)) {
$fileInfo = pathinfo( $fullpath );
$baseName = $fileInfo['basename'];
$r = '0001';
copyFilesFrmSrcToDst($baseName, $r, $fileInfo);
}else if(file_exists($fullpath)) {
$fileInfo = pathinfo( $fullpath );
$fileName = $fileInfo['filename'];
$r = '0001';
renameFileName($fileName,$r,$fileInfo);
} else {
}
echo json_encode(['status'=>true, 'msg'=>"Successfully completed"]);
}
function copyFilesFrmSrcToDst($bName, $n, $fileInfo) {
$fileArr = explode("_", $bName);
$n = str_pad($n, 4, '0', STR_PAD_LEFT);
if(is_dir($fileArr[0]."_".$n)) {
$n=$n+1;
copyFilesFrmSrcToDst($bName, $n, $fileInfo);
} else{
$folderName = $fileInfo['basename'];
$newFolderName = $folderName."_".$n;
$existingPath = $fileInfo['dirname']."/".$fileInfo['basename']."/";
mkdir($newFolderName);
copyr($existingPath,$newFolderName);
return true;
}
}
function copyr($source, $dest)
{
// Check for symlinks
if (is_link($source)) {
return symlink(readlink($source), $dest);
}
// Simple copy for a file
if (is_file($source)) {
return copy($source, $dest);
}
// Make destination directory
if (!is_dir($dest)) {
mkdir($dest);
}
// Loop through the folder
$dir = dir($source);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == '.' || $entry == '..') {
continue;
}
// Deep copy directories
copyr("$source/$entry", "$dest/$entry");
}
// Clean up
$dir->close();
return true;
}
function renameFileName($fname, $n, $fileInfo) {
$fileArr = explode("_", $fname);
$n = str_pad($n, 4, '0', STR_PAD_LEFT);
$fileName = $fileInfo['filename'];
if(file_exists($fileName."_".$n.".".$fileInfo['extension'])) {
$n = $n+1;
renameFileName($fname, $n, $fileInfo);
} else{
$fileName = $fileInfo['filename'];
$extistingFileName = $fileName.".".$fileInfo['extension'];
$newFileName = $fileName."_".$n.".".strtolower($fileInfo['extension']);
copy($extistingFileName, $newFileName);
return true;
}
}
exit;
?>