laravel 如何在laravel存储中查找没有特定文件扩展名的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39071938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find file without specific file extension in laravel storage?
提问by John Roca
How to find file by name without specific extension in laravel Storage?
如何在laravel Storage中按名称查找没有特定扩展名的文件?
like this "filename.*"
像这样 "filename.*"
Storage::get("filename.*")
I tried this but seems not to work. It searches for specific file with specific extension.
我试过这个,但似乎不起作用。它搜索具有特定扩展名的特定文件。
回答by jedrzej.kurylo
Storage::get()takes a file path as a parameter and returns the content of a single file identified by this path or throws FileNotFoundExceptionif file can't be found.
Storage::get()将文件路径作为参数并返回由该路径标识的单个文件的内容,如果找不到文件则抛出FileNotFoundException。
Wildcards are not supported in the path- one reason for that could be that there might be multiple files that match the path with wildcards which would break the rule that content of a single file is returned from Storage::get(). Scanning the whole folder would also be much slower, especially with remote storages.
路径中不支持通配符- 原因之一可能是可能有多个文件将路径与通配符匹配,这会破坏从Storage::get()返回单个文件内容的规则。扫描整个文件夹也会慢得多,尤其是远程存储。
However, you could get what you want using other functionality that Storagefacade offers. First, list the content of your storage - that will give you the list of all available files. Then filter the list yourself to get the list of matching files.
但是,您可以使用Storagefacade 提供的其他功能来获得您想要的东西。首先,列出您的存储内容 - 这将为您提供所有可用文件的列表。然后自己过滤列表以获取匹配文件的列表。
// list all filenames in given path
$allFiles = Storage::files('');
// filter the ones that match the filename.*
$matchingFiles = preg_grep('/^filename\./', $allFiles);
// iterate through files and echo their content
foreach ($matchingFiles as $path) {
echo Storage::get($path);
}
回答by Tomas
Dont trust what you see. Get inside and get the ext for your file
不要相信你所看到的。进入并获取文件的 ext
$pic = 'url/your.file';
$ext = image_type_to_mime_type(exif_imagetype($pic));
$ext = explode('/',$ext);
echo $ext[1];
回答by mayid
Accepted solution works. However I've found this other and I like it more:
接受的解决方案有效。但是我发现了另一个并且我更喜欢它:
$matchingFiles = \Illuminate\Support\Facades\File::glob("{$path}/*.log");
See reference here: http://laravel-recipes.com/recipes/143/finding-files-matching-a-pattern
请参阅此处的参考:http: //laravel-recipes.com/recipes/143/finding-files-matching-a-pattern