Functions for some idea, not the best code, but may have fast read/write if thousands of files, not tested:
(Maybe using $GLOBALS is not good here, learn good cooding practices)
<?php
(function () {
$pre = 'func__storage__';
$GLOBALS[APP][$pre . 'divide'] = [
// contains folder paths for which "divide in subfolders" is enabled for performance
'single' => [
'car-names/',
'vid-html-cache/'
],
'json' => [
'vid-html-cache__info/'
],
'php-array' => [] // Only option to read this format
];
$GLOBALS[APP][$pre . 'getPathParts'] = function ($path) {
$lastSlashIndex = strrpos($path, '/');
if ($lastSlashIndex === false) {
$folderPath = '';
$fileName = $path;
} else {
$folderPath = substr($path, 0, $lastSlashIndex + 1);
$fileName = substr($path, $lastSlashIndex + 1);
}
return [
'folderPath' => $folderPath,
'fileName' => $fileName,
];
};
$GLOBALS[APP][$pre . 'getDividePath'] = function ($fileName) {
$fileNameLen = strlen($fileName);
$divisionPath = $fileName[0] . '/';
if ($fileNameLen > 1) {
$divisionPath .= $fileName[1] . '/';
if ($fileNameLen > 2) {
$divisionPath .= $fileName[2] . '/';
if ($fileNameLen > 3) $divisionPath .= $fileName[3] . '/';
}
}
return $divisionPath;
};
$GLOBALS[APP][$pre . 'get'] = function ($p1) use ($pre) {
if (
!is_array($p1) ||
!isset($p1['path']) ||
!is_string($p1['path']) ||
(isset($p1['type']) && ($p1['type'] !== 'json' && $p1['type'] !== 'php-array'))
) logNDie('$GLOBALS[APP]["func__storage__get"], $p1 required array keys/vals not set/valid');
if (!isset($p1['type']) || $p1['type'] === 'single') { //assume its single
$p1['type'] = 'single';
$extension = '';
} elseif ($p1['type'] === 'json') $extension = '';
elseif ($p1['type'] === 'php-array') $extension = '.php';
$p1['path'] = $GLOBALS[APP][$pre . 'getPathParts']($p1['path']);
if (in_array($p1['path']['folderPath'], $GLOBALS[APP][$pre . 'divide'][$p1['type']]))
$p1['path']['folderPath'] .= $GLOBALS[APP][$pre . 'getDividePath']($p1['path']['fileName']);
$name = 'storage/' . $p1['type'] . '/' . $p1['path']['folderPath'] . $p1['path']['fileName'] . $extension;
if ($p1['type'] === 'php-array') $toReturn = @include $name;
else $toReturn = @file_get_contents($name);
if ($p1['type'] === 'json') $toReturn = json_decode($toReturn, true);
return $toReturn;
};
$GLOBALS[APP][$pre . 'set'] = function ($p1) use ($pre) { // array:$p1, keys:storageRoot(optional),type(optional),path,data
if (
!is_array($p1) ||
!isset($p1['path']) ||
!is_string($p1['path']) ||
!isset($p1['data']) ||
(isset($p1['type']) && ($p1['type'] !== 'json'))
) logNDie('$GLOBALS[APP]["func__storage__set"], $p1 required array keys/vals not set/valid');
if (!isset($p1['type']) || $p1['type'] === 'single') { //assume its single
$p1['type'] = 'single';
if (!is_string($p1['data'])) logNDie('$GLOBALS[APP]["func"]["storage"]["write"], string not given for type "single"');
$extension = '';
} elseif ($p1['type'] === 'json') {
if (!is_array($p1['data'])) logNDie('$GLOBALS[APP]["func"]["storage"]["write"], array not given for type "json"');
$p1['data'] = json_encode($p1['data']);
$extension = '';
}
$p1['storageRoot'] = $p1['storageRoot'] ?? 'storage/';
$p1['path'] = $GLOBALS[APP][$pre . 'getPathParts']($p1['path']);
if (in_array($p1['path']['folderPath'], $GLOBALS[APP][$pre . 'divide'][$p1['type']]))
$p1['path']['folderPath'] .= $GLOBALS[APP][$pre . 'getDividePath']($p1['path']['fileName']);
$folderPath = $p1['storageRoot'] . $p1['type'] . '/' . $p1['path']['folderPath'];
if (!file_exists($folderPath)) mkdir($folderPath, 0775, true);
return file_put_contents($folderPath . $p1['path']['fileName'] . $extension, $p1['data']);
};
$GLOBALS[APP][$pre . 'isPresent'] = function ($p1) use ($pre) {
if (
!is_array($p1) ||
!isset($p1['path']) ||
!is_string($p1['path']) ||
(isset($p1['type']) && ($p1['type'] !== 'json' && $p1['type'] !== 'php-array'))
) logNDie('$GLOBALS[APP]["func__storage__isPresent"], $p1 required array keys/vals not set/valid');
if (!isset($p1['type']) || $p1['type'] === 'single') { //assume its single
$p1['type'] = 'single';
$extension = '';
} elseif ($p1['type'] === 'json') $extension = '';
elseif ($p1['type'] === 'php-array') $extension = '.php';
$p1['path'] = $GLOBALS[APP][$pre . 'getPathParts']($p1['path']);
if (in_array($p1['path']['folderPath'], $GLOBALS[APP][$pre . 'divide'][$p1['type']]))
$p1['path']['folderPath'] .= $GLOBALS[APP][$pre . 'getDividePath']($p1['path']['fileName']);
return file_exists('storage/' . $p1['type'] . '/' . $p1['path']['folderPath'] . $p1['path']['fileName'] . $extension);
};
})();
Usage Example:
$GLOBALS[APP]['func__storage__get'](['path' => 'test']);
$GLOBALS[APP]['func__storage__isPresent'](['path' => 'car-names/audi']);