Files
2025-08-02 16:30:27 +02:00

64 lines
1.7 KiB
PHP

<?php
namespace KupShop\KupShopBundle\Util;
class FileUtil
{
public static function isFileNewer($younger, $older, &$time_younger = null, &$time_older = null)
{
$time_younger = @filemtime($younger);
if ($time_younger === false) {
return false;
}
$time_older = @filemtime($older);
return $time_younger > $time_older;
}
// Delete all folders and files in given dir
public static function deleteDir($dir)
{
$structure = glob(rtrim($dir, '/').'/*');
if (is_array($structure)) {
foreach ($structure as $file) {
if (is_dir($file)) {
self::deleteDir($file);
} elseif (is_file($file)) {
unlink($file);
}
}
}
rmdir($dir);
}
public static function loadCSV(string $file, string $separator = ','): array
{
$handle = fopen($file, 'r');
$result = [];
while (($data = fgetcsv($handle, 9999, $separator)) !== false) {
$result[] = $data;
}
fclose($handle);
return $result;
}
public static function unzip(string $file, string $destination): array
{
$zip = new \ZipArchive();
if ($zip->open($file) === true) {
$zip->extractTo($destination);
$zip->close();
return array_values(array_map(
fn ($x) => rtrim($destination, '/').'/'.$x,
array_filter(scandir($destination), fn ($x) => !empty($x) && !in_array($x, ['.', '..']))
));
}
throw new \RuntimeException("Cannot unzip file: {$file}");
}
}