在Laravel中找到目录中的最小文件大小?

如何解决在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...

解决方法

在循环文件时保持最小:

-- The C compiler identification is GNU 8.3.0
-- The CXX compiler identification is GNU 8.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PythonLibs: /usr/lib/python3.7/config-3.7m-arm-linux-gnueabihf/libpython3.7.so (found suitable version "3.7.3",minimum required is "3.5") 
-- Using libclang archive: /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/cpp/../clang_archives/libclang-10.0.0-armv7a-linux-gnueabihf.tar.bz2
-- Using libclang to provide semantic completion for C/C++/ObjC
-- Using external libclang: /tmp/ycm_build_zcxr6q5l/lib/libclang.so.10
-- NOT using clang-tidy for static analysis.
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/ycm_build_zcxr6q5l
Scanning dependencies of target BoostParts
[  2%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/filesystem/src/codecvt_error_category.cpp.o
[  5%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/filesystem/src/directory.cpp.o
[  8%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/filesystem/src/exception.cpp.o
[ 11%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/filesystem/src/operations.cpp.o
[ 13%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/filesystem/src/path.cpp.o
[ 16%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/filesystem/src/path_traits.cpp.o
[ 19%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/filesystem/src/portability.cpp.o
[ 22%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/filesystem/src/unique_path.cpp.o
[ 25%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/filesystem/src/utf8_codecvt_facet.cpp.o
[ 27%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/filesystem/src/windows_file_codecvt.cpp.o
[ 30%] Linking CXX static library libBoostParts.a
[ 30%] Built target BoostParts
Scanning dependencies of target ycm_core
[ 33%] Building CXX object ycm/CMakeFiles/ycm_core.dir/Candidate.cpp.o
[ 36%] Building CXX object ycm/CMakeFiles/ycm_core.dir/CandidateRepository.cpp.o
[ 38%] Building CXX object ycm/CMakeFiles/ycm_core.dir/Character.cpp.o
[ 41%] Building CXX object ycm/CMakeFiles/ycm_core.dir/CharacterRepository.cpp.o
[ 44%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/ClangCompleter.cpp.o
[ 47%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/ClangHelpers.cpp.o
[ 50%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/ClangUtils.cpp.o
[ 52%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/CompilationDatabase.cpp.o
[ 55%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/CompletionData.cpp.o
[ 58%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/Documentation.cpp.o
[ 61%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/Range.cpp.o
[ 63%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/TranslationUnit.cpp.o
[ 66%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/TranslationUnitStore.cpp.o
[ 69%] Building CXX object ycm/CMakeFiles/ycm_core.dir/CodePoint.cpp.o
[ 72%] Building CXX object ycm/CMakeFiles/ycm_core.dir/CodePointRepository.cpp.o
[ 75%] Building CXX object ycm/CMakeFiles/ycm_core.dir/IdentifierCompleter.cpp.o
[ 77%] Building CXX object ycm/CMakeFiles/ycm_core.dir/IdentifierDatabase.cpp.o
[ 80%] Building CXX object ycm/CMakeFiles/ycm_core.dir/IdentifierUtils.cpp.o
[ 83%] Building CXX object ycm/CMakeFiles/ycm_core.dir/PythonSupport.cpp.o
[ 86%] Building CXX object ycm/CMakeFiles/ycm_core.dir/Result.cpp.o
[ 88%] Building CXX object ycm/CMakeFiles/ycm_core.dir/Utils.cpp.o
[ 91%] Building CXX object ycm/CMakeFiles/ycm_core.dir/Word.cpp.o
[ 94%] Building CXX object ycm/CMakeFiles/ycm_core.dir/versioning.cpp.o
[ 97%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ycm_core.cpp.o
[100%] Linking CXX shared library /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/ycm_core.so
[100%] Built target ycm_core
-- The C compiler identification is GNU 8.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Found PythonLibs: /usr/lib/python3.7/config-3.7m-arm-linux-gnueabihf/libpython3.7.so (found version "3.7.3") 
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/regex_build_x5t1av6p
Scanning dependencies of target _regex
[ 33%] Building C object CMakeFiles/_regex.dir/regex_3/_regex.c.o
[ 66%] Building C object CMakeFiles/_regex.dir/regex_3/_regex_unicode.c.o
[100%] Linking C shared library /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/cregex/regex_3/_regex.so
[100%] Built target _regex
running build
running build_py
creating /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3
creating /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog
copying src/watchdog/events.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog
copying src/watchdog/watchmedo.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog
copying src/watchdog/__init__.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog
copying src/watchdog/version.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog
creating /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/utils
copying src/watchdog/utils/compat.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/utils
copying src/watchdog/utils/win32stat.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/utils
copying src/watchdog/utils/dirsnapshot.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/utils
copying src/watchdog/utils/delayed_queue.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/utils
copying src/watchdog/utils/echo.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/utils
copying src/watchdog/utils/bricks.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/utils
copying src/watchdog/utils/unicode_paths.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/utils
copying src/watchdog/utils/__init__.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/utils
copying src/watchdog/utils/platform.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/utils
creating /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/tricks
copying src/watchdog/tricks/__init__.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/tricks
creating /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/observers
copying src/watchdog/observers/inotify_c.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/observers
copying src/watchdog/observers/inotify_buffer.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/observers
copying src/watchdog/observers/fsevents.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/observers
copying src/watchdog/observers/polling.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/observers
copying src/watchdog/observers/inotify.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/observers
copying src/watchdog/observers/api.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/observers
copying src/watchdog/observers/read_directory_changes.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/observers
copying src/watchdog/observers/kqueue.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/observers
copying src/watchdog/observers/__init__.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/observers
copying src/watchdog/observers/fsevents2.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/observers
copying src/watchdog/observers/winapi.py -> /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/watchdog_deps/watchdog/build/lib3/watchdog/observers
running egg_info
writing src/watchdog.egg-info/PKG-INFO
writing dependency_links to src/watchdog.egg-info/dependency_links.txt
writing entry points to src/watchdog.egg-info/entry_points.txt
writing requirements to src/watchdog.egg-info/requires.txt
writing top-level names to src/watchdog.egg-info/top_level.txt
reading manifest file 'src/watchdog.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching '*.h' under directory 'src'
writing manifest file 'src/watchdog.egg-info/SOURCES.txt'
Searching Python 3.7 libraries...
Found Python library: /usr/lib/python3.7/config-3.7m-arm-linux-gnueabihf/libpython3.7.so
Found Python headers folder: /usr/include/python3.7m
Installing Omnisharp v1.35.3
Using cached Omnisharp: omnisharp.http-linux-x86.tar.gz
Extracting Omnisharp to /home/pi/.vim/plugged/youcompleteme/third_party/ycmd/third_party/omnisharp-roslyn...DONE
Done installing Omnisharp
go: finding github.com/kr/pty v1.1.1
go: finding golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e
go: finding gopkg.in/errgo.v2 v2.1.0
go: finding golang.org/x/tools v0.0.0-20200701133321-6ddc6be4d35f
go: finding golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d
go: finding golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529
go: finding gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127
go: finding golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3
go: finding golang.org/x/sys v0.0.0-20190412213103-97732733099d
go: finding golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
go: finding golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a
go: finding golang.org/x/text v0.3.0
go: finding golang.org/x/mod v0.2.0
go: finding golang.org/x/net v0.0.0-20200226121028-0de0cce0169b
go: finding golang.org/x/net v0.0.0-20190620200207-3b0461eec859
go: finding github.com/yuin/goldmark v1.1.27
go: finding golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e
go: finding golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
go: finding golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
go: finding golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
go: finding golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898
go: finding golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
go: finding golang.org/x/sync v0.0.0-20190423024810-112230192c58
go: downloading golang.org/x/tools v0.0.0-20200701133321-6ddc6be4d35f
go: downloading golang.org/x/tools/gopls v0.4.2
go: downloading honnef.co/go/tools v0.0.1-2020.1.4
go: downloading github.com/sergi/go-diff v1.1.0
go: downloading golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
go: downloading mvdan.cc/xurls/v2 v2.2.0
go: downloading golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
go: downloading golang.org/x/mod v0.2.0
go: downloading github.com/BurntSushi/toml v0.3.1
# golang.org/x/tools/internal/lsp/analysis/fillstruct
third_party/go/pkg/mod/golang.org/x/tools@v0.0.0-20200701133321-6ddc6be4d35f/internal/lsp/analysis/fillstruct/fillstruct.go:141:14: tok.LineStart undefined (type *token.File has no field or method LineStart)
third_party/go/pkg/mod/golang.org/x/tools@v0.0.0-20200701133321-6ddc6be4d35f/internal/lsp/analysis/fillstruct/fillstruct.go:170:15: tok.LineStart undefined (type *token.File has no field or method LineStart)
third_party/go/pkg/mod/golang.org/x/tools@v0.0.0-20200701133321-6ddc6be4d35f/internal/lsp/analysis/fillstruct/fillstruct.go:172:15: tok.LineStart undefined (type *token.File has no field or method LineStart)
# golang.org/x/tools/internal/jsonrpc2
third_party/go/pkg/mod/golang.org/x/tools@v0.0.0-20200701133321-6ddc6be4d35f/internal/jsonrpc2/messages.go:171:5: undefined: errors.As
third_party/go/pkg/mod/golang.org/x/tools@v0.0.0-20200701133321-6ddc6be4d35f/internal/jsonrpc2/serve.go:129:5: undefined: errors.Is
,

我认为您可以将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]);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-