亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在帶有 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 arrayforeach ($files as $key => $file){    $files[$key] = ['file' => $file, 'size' => Storage::size($file)]; }$min = collect($files)->min('size'); // 1000000// find file by size...
查看完整描述

2 回答

?
PIPIONE

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]);


查看完整回答
反對 回復 2023-10-22
?
慕容森

TA貢獻1853條經驗 獲得超18個贊

循環文件時保持最?。?/p>


$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



查看完整回答
反對 回復 2023-10-22
  • 2 回答
  • 0 關注
  • 182 瀏覽

添加回答

了解更多

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號