|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace XoopsModules\Tdmcreate\Files; |
|
4
|
|
|
use XoopsModules\Tdmcreate; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class CreateClone |
|
8
|
|
|
*/ |
|
9
|
|
|
class CreateClone |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Delete a file or recursively delete a directory |
|
13
|
|
|
* |
|
14
|
|
|
* @param string $path Path to file or directory |
|
15
|
|
|
|
|
16
|
|
|
public static function deleteFileFolder($path) { |
|
17
|
|
|
|
|
18
|
|
|
if (is_file($path)) { |
|
19
|
|
|
return @unlink($path); |
|
20
|
|
|
} |
|
21
|
|
|
elseif (is_dir($path)) { |
|
22
|
|
|
$scan = glob(rtrim($path,'/').'/*'); |
|
23
|
|
|
foreach($scan as $index=>$path) { |
|
24
|
|
|
self::deleteFileFolder($path); |
|
25
|
|
|
} |
|
26
|
|
|
return @rmdir($path); |
|
27
|
|
|
} |
|
28
|
|
|
}*/ |
|
29
|
|
|
|
|
30
|
|
|
// recursive cloning script |
|
31
|
|
|
/** |
|
32
|
|
|
* @param $src_path |
|
33
|
|
|
* @param $dst_path |
|
34
|
|
|
* @param bool $replace_code |
|
35
|
|
|
* @param array $patKeys |
|
36
|
|
|
* @param array $patValues |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function cloneFileFolder($src_path, $dst_path, $replace_code = false, $patKeys = [], $patValues =[]) |
|
39
|
|
|
{ |
|
40
|
|
|
// open the source directory |
|
41
|
|
|
$dir = opendir($src_path); |
|
42
|
|
|
// Make the destination directory if not exist |
|
43
|
|
|
@mkdir($dst_path); |
|
|
|
|
|
|
44
|
|
|
// Loop through the files in source directory |
|
45
|
|
|
while( $file = readdir($dir) ) { |
|
|
|
|
|
|
46
|
|
|
if (( $file != '.' ) && ( $file != '..' )) { |
|
47
|
|
|
if ( is_dir($src_path . '/' . $file) ) { |
|
48
|
|
|
// Recursively calling custom copy function for sub directory |
|
49
|
|
|
self::cloneFileFolder($src_path . '/' . $file, $dst_path . '/' . $file, $replace_code, $patKeys, $patValues); |
|
50
|
|
|
} else { |
|
51
|
|
|
self::cloneFile($src_path . '/' . $file, $dst_path . '/' . $file, $replace_code, $patKeys, $patValues); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
closedir($dir); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param $src_file |
|
60
|
|
|
* @param $dst_file |
|
61
|
|
|
* @param bool $replace_code |
|
62
|
|
|
* @param array $patKeys |
|
63
|
|
|
* @param array $patValues |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function cloneFile($src_file, $dst_file, $replace_code = false, $patKeys = [], $patValues =[]) |
|
66
|
|
|
{ |
|
67
|
|
|
if ($replace_code) { |
|
68
|
|
|
$noChangeExtensions = ['jpeg', 'jpg', 'gif', 'png', 'zip', 'ttf']; |
|
69
|
|
|
if (in_array(mb_strtolower(pathinfo($src_file, PATHINFO_EXTENSION)), $noChangeExtensions)) { |
|
70
|
|
|
// image |
|
71
|
|
|
copy($src_file, $dst_file); |
|
72
|
|
|
} else { |
|
73
|
|
|
// file, read it |
|
74
|
|
|
$content = file_get_contents($src_file); |
|
75
|
|
|
$content = str_replace($patKeys, $patValues, $content); |
|
76
|
|
|
file_put_contents($dst_file, $content); |
|
77
|
|
|
} |
|
78
|
|
|
} else { |
|
79
|
|
|
copy($src_file, $dst_file); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: