【问题标题】:Get all images name from folder using link in controller for JSON response laravel 5.1使用控制器中的链接从文件夹中获取所有图像名称以获取 JSON 响应 laravel 5.1
【发布时间】:2024-05-01 19:40:02
【问题描述】:

数据库有文件夹的链接,文件夹包含许多图像。我想获取所有图像名称,但 glob()(函数)不起作用..

function index($data){
        $users = DB::select('select * from `title` INNER JOIN `title_img` on `title`.title_no = `title_img`.title_no AND `title`.title_name = ?', [$data]);
        $ab = array();
        foreach ($users as $user) {
            $name = $user->img_folder;
            $nam = 'http://127.0.0.1/shaadi/public/data/'.$name;
            $ab[] = glob($name);
        }
    var_dump($ab);
    var_dump($nam);
        return response()->json($users);
    }

输出

   <pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=3)</i>
  0 <font color='#888a85'>=&gt;</font> 
    <b>array</b> <i>(size=0)</i>
      <i><font color='#888a85'>empty</font></i>
  1 <font color='#888a85'>=&gt;</font> 
    <b>array</b> <i>(size=0)</i>
      <i><font color='#888a85'>empty</font></i>
  2 <font color='#888a85'>=&gt;</font> 
    <b>array</b> <i>(size=0)</i>
      <i><font color='#888a85'>empty</font></i>
</pre><pre class='xdebug-var-dump' dir='ltr'><small>string</small> <font color='#cc0000'>'http://127.0.0.1/shaadi/public/data/image/caterer/standard/'</font> <i>(length=59)</i>
</pre>[{"title_no":1,"title_name":"Catering","title_description":"Let your taste buds loose!<br\/>We promise delicious high quality and hygienic delicious for you.","img_folder":"image\/caterer\/luxury\/"},{"title_no":1,"title_name":"Catering","title_description":"Let your taste buds loose!<br\/>We promise delicious high quality and hygienic delicious for you.","img_folder":"image\/caterer\/premium\/"},{"title_no":1,"title_name":"Catering","title_description":"Let your taste buds loose!<br\/>We promise delicious high quality and hygienic delicious for you.","img_folder":"image\/caterer\/standard\/"}]

【问题讨论】:

  • img_folder 在您的文件系统中位于何处?此外,您使用 Laravel 通过使用它提供给您的功能来接受它laravel.com/docs/5.1/filesystem
  • img_folder 是数据库中的列。链接“image/caterer/luxury/”写在哪里.....这个图片文件夹是public/data/image/caterer....
  • $nam = '127.0.0.1/shaadi/public/data/image/caterer/standard ; $ab[] = 存储::allFiles($nam);我需要 $nam 路径中的所有文件
  • 如果您尝试使用 $nam 变量作为获取文件的参数,与 glob 示例中的 $name 相比,会有很大的不同。如果您尝试从 URL 中提取图像列表,那是完全不同的事情,而不是 globStorage::allFiles 会做什么。
  • 其实我在例子中写错了……那是 glob($nam);

标签: php json image laravel directory


【解决方案1】:

你可以改成

    foreach ($users as $user) {
        $name = $user->img_folder;
        $nam = 'http://127.0.0.1/shaadi/public/data/'.$name;
        $ab[] = glob($name);
    }

这个

foreach ($users as $user) {
    $name = $user->img_folder;
    $nam = public_path('data/' . $name . DIRECTORY_SEPARATOR . '*');//getting real path
    $ab[] = \Illuminate\Support\Facades\File::glob($nam);//Matching all files
}

但请记住,这只会获取目录的内容,而不是 recursively

要获取 URL 路径,您必须 str_replace 类似的路径

str_replace(public_path(), '', $path)

& 你也应该应用asset 函数

asset(str_replace(public_path(), '', $path))

现在,如果您要返回包含多维数组中所有路径的 $ab 变量,那么您可以循环或创建递归函数来实现此目的,或者您可以在 $ab 中设置值时这样做

$ab[] = collect(\Illuminate\Support\Facades\File::glob($nam))->map(function ($path)//this will convert array to laravel collection
        {
            return asset(str_replace(public_path(), '', $path));//converts to url
        })->filter(function ($path)
        {
            return $path;//removes any empty value
        })->values()//converts to values
        ->toArray();//converts to to array

这是我的项目中经过修改的示例代码,虽然我没有针对您的目的进行测试,但它应该可以工作。

【讨论】:

  • @SachinMarwha 我已经编辑了我的答案,你可以检查一下。