php - 如何在laravel中获取数组元素的文件扩展名
问题描述
我正在尝试获取数据库中图像列的文件扩展名,但出现此错误:
pathinfo() 期望参数 1 是字符串,给定数组。
这是我的控制器代码如下:
$news = DB::table('news')
->select('image')
->where('id', '=', 'user_id')
->get();
$file = $news;
$ext = pathinfo($file, PATHINFO_EXTENSION);
return view('pages.index',compact('ext'));
这是我的看法:
@if($ext == 'mp4' || $ext == 'mov' || $ext == 'vob' || $ext == 'mpeg' || $ext == '3gp' || $ext == 'avi' || $ext == 'wmv' || $ext == 'mov' || $ext == 'amv' || $ext == 'svi' || $ext == 'flv' || $ext == 'mkv' || $ext == 'webm' || $ext == 'gif' || $ext == 'asf')
<video width="320" height="240" controls>
<source src="{{URL::asset('upload/news/'.$topnews->image) }}" type="video/mp4">
Your browser does not support the video tag.
</video>
@else
<img src="{{ URL::asset('upload/news/'.$topnews->image) }}" class="img-responsive" alt="" width="320" height="240">
@endif
这是我的路线:
Route::get('/', 'IndexController@index');
解决方案
遍历您的数组并应用于数组pathinfo
的每个元素:
$extensions = [];
foreach ($news as $file) {
$extensions[$file] = pathinfo($file, PATHINFO_EXTENSION);
}
推荐阅读
- angular - 成功构建Angular后需要将dist文件夹复制到另一个文件夹
- mysql - 我需要使用 sql 查询根据 orderId 获取最后创建的合格乘客 ID 和 ping 乘客 ID
- php - 使用 php 和 VS Code 在 mysql 中显示 مريÙ
- node.js - 如何找到特定的 _id 并从对象数组中更新它?
- java - createNativequery 中的 sql 注入
- javascript - 根据文本区域值附加行和标签jQuery(降低可重复性)
- hive - S3 选择与 Presto
- python - 如何仅在边界框内找到光流?
- python - numpy.ndarray' 对象没有属性 'y_pred
- python - python - 如何对包含在python中遵循JSON格式的列表表示形式的字符串进行排序