2 回答
TA贡献1829条经验 获得超9个赞
我认为您可以使用回调根据文件大小对文件进行排序。也许是这样的东西,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]);
TA贡献1853条经验 获得超18个赞
循环文件时保持最小:
$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
- 2 回答
- 0 关注
- 122 浏览
添加回答
举报