-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetFileSize.php
66 lines (59 loc) · 1.71 KB
/
getFileSize.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
<?php
/**
* getFileSize
*
* Gets the file size of a static resource
*
* @var modX $modx
* @var array $scriptProperties
*
* Parameters:
* @property integer $id (required) - ID of the static resource
* @property string $format (optional) - Output format: 'raw', 'formatted', or 'both'. Default: 'formatted'
*
* Example usage:
* [[getFileSize? &id=`123`]]
* [[getFileSize? &id=`123` &format=`raw`]]
*/
// Get parameters
$resourceId = $modx->getOption('id', $scriptProperties);
$format = $modx->getOption('format', $scriptProperties, 'formatted');
// Validate resource ID
if (!$resourceId) {
return 'Error: Resource ID not specified';
}
// Get the resource
$resource = $modx->getObject('modStaticResource', $resourceId);
if (!$resource) {
return 'Error: Resource not found';
}
// Get the source file path
$sourcePath = $resource->get('content');
if (!$sourcePath || !file_exists($sourcePath)) {
return 'Error: File not found';
}
// Get file size in bytes
$sizeInBytes = filesize($sourcePath);
// Format size to human readable format
if ($sizeInBytes >= 1073741824) {
$formattedSize = number_format($sizeInBytes / 1073741824, 2) . ' GB';
} elseif ($sizeInBytes >= 1048576) {
$formattedSize = number_format($sizeInBytes / 1048576, 2) . ' MB';
} elseif ($sizeInBytes >= 1024) {
$formattedSize = number_format($sizeInBytes / 1024, 2) . ' KB';
} else {
$formattedSize = $sizeInBytes . ' bytes';
}
// Return based on format parameter
switch ($format) {
case 'raw':
return $sizeInBytes;
case 'both':
return json_encode([
'bytes' => $sizeInBytes,
'formatted' => $formattedSize
]);
case 'formatted':
default:
return $formattedSize;
}