在具有 Laravel 的目录中查找最小文件大小?
Find lowest file size in a directory with Laravel?
我需要从目录中找到最小的文件大小。使用 Laravel 并尽可能减少代码的最佳方法是什么?
我正在使用这段代码,但我确信有更好更有效的方法。
$files = Storage::files("videos");
$files = Arr::where($files, function ($value, $key) {
return Str::contains($value, 'string..') && Str::endsWith($value ,'.mp4');
}); // keep only certain files from videos directory in the files array
foreach ($files as $key => $file)
{
$files[$key] = ['file' => $file, 'size' => Storage::size($file)];
}
$min = collect($files)->min('size'); // 1000000
// find file by size...
循环文件时保持最小:
$files = Storage::files("videos");
$files = Arr::where($files, function ($value, $key) {
return Str::contains($value, 'string..') && Str::endsWith($value ,'.mp4');
}); // keep only certain files from videos directory in the files array
$minSize = PHP_INT_MAX;
$minFile = null;
foreach ($files as $key => $file)
{
$size = Storage::size($file);
if ($size < $minSize) {
$minSize = $size;
$minFile = $file;
}
$files[$key] = ['file' => $file, 'size' => $size];
}
// $minSize is the smallest size in the directory and $minFile is the file with the smallest size
// $minFile will be `null` if there are no files in the initial array, you should add a check for this possibility
我认为您可以使用 sortBy
和回调来根据文件大小对文件进行排序。也许是这样的,
//after Arr::where
$fileWithSmallestSize = collect($files)->sortBy(function($file){
return Storage::size($file);
})->first();
另一种选择是减少收集。
//after Arr::where
$fileWithSmallestSize = collect($files)->reduce(function($smallestFile, $file){
return ( Storage::size($file) < Storage::size($smallestFile) )? $file : $smallestFile;
}, $files[0]);
我需要从目录中找到最小的文件大小。使用 Laravel 并尽可能减少代码的最佳方法是什么?
我正在使用这段代码,但我确信有更好更有效的方法。
$files = Storage::files("videos");
$files = Arr::where($files, function ($value, $key) {
return Str::contains($value, 'string..') && Str::endsWith($value ,'.mp4');
}); // keep only certain files from videos directory in the files array
foreach ($files as $key => $file)
{
$files[$key] = ['file' => $file, 'size' => Storage::size($file)];
}
$min = collect($files)->min('size'); // 1000000
// find file by size...
循环文件时保持最小:
$files = Storage::files("videos");
$files = Arr::where($files, function ($value, $key) {
return Str::contains($value, 'string..') && Str::endsWith($value ,'.mp4');
}); // keep only certain files from videos directory in the files array
$minSize = PHP_INT_MAX;
$minFile = null;
foreach ($files as $key => $file)
{
$size = Storage::size($file);
if ($size < $minSize) {
$minSize = $size;
$minFile = $file;
}
$files[$key] = ['file' => $file, 'size' => $size];
}
// $minSize is the smallest size in the directory and $minFile is the file with the smallest size
// $minFile will be `null` if there are no files in the initial array, you should add a check for this possibility
我认为您可以使用 sortBy
和回调来根据文件大小对文件进行排序。也许是这样的,
//after Arr::where
$fileWithSmallestSize = collect($files)->sortBy(function($file){
return Storage::size($file);
})->first();
另一种选择是减少收集。
//after Arr::where
$fileWithSmallestSize = collect($files)->reduce(function($smallestFile, $file){
return ( Storage::size($file) < Storage::size($smallestFile) )? $file : $smallestFile;
}, $files[0]);