|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BPT\tools; |
|
4
|
|
|
|
|
5
|
|
|
use BPT\constants\loggerTypes; |
|
6
|
|
|
use BPT\exception\bptException; |
|
7
|
|
|
use BPT\logger; |
|
8
|
|
|
use FilesystemIterator; |
|
9
|
|
|
use RecursiveDirectoryIterator; |
|
10
|
|
|
use RecursiveIteratorIterator; |
|
11
|
|
|
use BPT\tools; |
|
12
|
|
|
use ZipArchive; |
|
13
|
|
|
|
|
14
|
|
|
trait file { |
|
15
|
|
|
/** |
|
16
|
|
|
* receive size from path(can be url or file path) |
|
17
|
|
|
* |
|
18
|
|
|
* NOTE : some url will not return real size! |
|
19
|
|
|
* |
|
20
|
|
|
* e.g. => tools::size('xFile.zip'); |
|
21
|
|
|
* |
|
22
|
|
|
* e.g. => tools::size(path: 'xFile.zip'); |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $path file path, could be url |
|
25
|
|
|
* @param bool $format if you set this true , you will receive symbolic string like 2.76MB for return |
|
26
|
|
|
* |
|
27
|
|
|
* @return string|int|false string for formatted data , int for normal data , false when size can not be found(file not found or ...) |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function size (string $path, bool $format = true): string|int|false { |
|
30
|
|
|
if (filter_var($path, FILTER_VALIDATE_URL)) { |
|
31
|
|
|
$ch = curl_init($path); |
|
32
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
33
|
|
|
curl_setopt($ch, CURLOPT_HEADER, true); |
|
34
|
|
|
curl_setopt($ch, CURLOPT_NOBODY, true); |
|
35
|
|
|
curl_exec($ch); |
|
36
|
|
|
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD); |
|
37
|
|
|
curl_close($ch); |
|
38
|
|
|
} |
|
39
|
|
|
else { |
|
40
|
|
|
$size = file_exists($path) ? filesize($path) : false; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if (isset($size) && is_numeric($size)) { |
|
44
|
|
|
return $format ? tools::byteFormat($size) : $size; |
|
45
|
|
|
} |
|
46
|
|
|
else return false; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Delete a folder or file if exist |
|
51
|
|
|
* |
|
52
|
|
|
* e.g. => tools::delete(path: 'xfolder/yfolder'); |
|
53
|
|
|
* |
|
54
|
|
|
* e.g. => tools::delete('xfolder/yfolder',false); |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $path folder or file path |
|
57
|
|
|
* @param bool $sub set true for removing subFiles too, if folder has subFiles and this set to false , you will receive error |
|
58
|
|
|
* |
|
59
|
|
|
* @return bool |
|
60
|
|
|
* @throws bptException |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function delete (string $path, bool $sub = true): bool { |
|
63
|
|
|
if (is_dir($path)) { |
|
64
|
|
|
if (count(scandir($path)) > 2) { |
|
65
|
|
|
if ($sub) { |
|
66
|
|
|
$it = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS); |
|
67
|
|
|
$files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST); |
|
68
|
|
|
foreach ($files as $file) { |
|
69
|
|
|
$file->isDir() ? rmdir($file->getRealPath()) : unlink($file->getRealPath()); |
|
70
|
|
|
} |
|
71
|
|
|
return rmdir($path); |
|
72
|
|
|
} |
|
73
|
|
|
else { |
|
74
|
|
|
logger::write("tools::delete function used\ndelete function cannot delete folder because its have subFiles and sub parameter haven't true value",loggerTypes::ERROR); |
|
75
|
|
|
throw new bptException('DELETE_FOLDER_HAS_SUB'); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
else return rmdir($path); |
|
79
|
|
|
} |
|
80
|
|
|
else return unlink($path); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* convert all files in selected path to zip and then save it in dest path |
|
85
|
|
|
* |
|
86
|
|
|
* e.g. => tools::zip('xFolder','yFolder/xFile.zip',false,true); |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $path your file or folder to be zipped |
|
89
|
|
|
* @param string $destination destination path for create file |
|
90
|
|
|
* @param bool $self set true for adding main folder to zip file |
|
91
|
|
|
* @param bool $sub_folder set false for not adding sub_folders and save all files in main folder |
|
92
|
|
|
* |
|
93
|
|
|
* @return bool |
|
94
|
|
|
* @throws bptException when zip extension not found |
|
95
|
|
|
*/ |
|
96
|
|
|
public static function zip (string $path, string $destination, bool $self = true, bool $sub_folder = true): bool { |
|
97
|
|
|
if (extension_loaded('zip')) { |
|
98
|
|
|
if (file_exists($destination)) unlink($destination); |
|
99
|
|
|
|
|
100
|
|
|
$path = realpath($path); |
|
101
|
|
|
$zip = new ZipArchive(); |
|
102
|
|
|
$zip->open($destination, ZipArchive::CREATE); |
|
103
|
|
|
|
|
104
|
|
|
if (is_dir($path)){ |
|
105
|
|
|
if ($self){ |
|
106
|
|
|
$dirs = explode('\\',$path); |
|
107
|
|
|
$dir_count = count($dirs); |
|
108
|
|
|
$main_dir = $dirs[$dir_count-1]; |
|
109
|
|
|
|
|
110
|
|
|
$path = ''; |
|
111
|
|
|
for ($i=0; $i < $dir_count - 1; $i++) { |
|
112
|
|
|
$path .= '\\' . $dirs[$i]; |
|
113
|
|
|
} |
|
114
|
|
|
$path = substr($path, 1); |
|
115
|
|
|
$zip->addEmptyDir($main_dir); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$it = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS); |
|
119
|
|
|
$files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::SELF_FIRST); |
|
120
|
|
|
foreach ($files as $file) { |
|
121
|
|
|
if ($file->isFile()){ |
|
122
|
|
|
if ($sub_folder){ |
|
123
|
|
|
$zip->addFile($file, str_replace($path . '\\', '', $file)); |
|
124
|
|
|
} |
|
125
|
|
|
else{ |
|
126
|
|
|
$zip->addFile($file, basename($file)); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
elseif ($file->isDir() && $sub_folder) { |
|
130
|
|
|
$zip->addEmptyDir(str_replace($path . '\\', '', $file . '\\')); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
else{ |
|
135
|
|
|
$zip->addFile($path, basename($path)); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $zip->close(); |
|
139
|
|
|
} |
|
140
|
|
|
else { |
|
141
|
|
|
logger::write("tools::zip function used\nzip extension is not found , It may not be installed or enabled",loggerTypes::ERROR); |
|
142
|
|
|
throw new bptException('ZIP_EXTENSION_MISSING'); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
} |