Php 使用用于JSON响应的控制器中的链接从文件夹中获取所有图像名称laravel 5.1

Php 使用用于JSON响应的控制器中的链接从文件夹中获取所有图像名称laravel 5.1,php,json,image,laravel,directory,Php,Json,Image,Laravel,Directory,数据库中有文件夹的链接,文件夹中包含许多图像。我想获取所有图像的名称,但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(

数据库中有文件夹的链接,文件夹中包含许多图像。我想获取所有图像的名称,但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\/"}]

数组(大小=3)
0 = 
数组(大小=0)
空的
1 = 
数组(大小=0)
空的
2 = 
数组(大小=0)
空的
字符串'http://127.0.0.1/shaadi/public/data/image/caterer/standard/'(长度=59)
[{“title\u no”:1,“title\u name”:“餐饮”,“title\u description”:“放开你的味蕾!我们承诺为你提供优质、卫生的美味”,“img\u文件夹”:“image\/caterer\/luxury\/”},{“title\u no”:1,“title\u name”:“餐饮”,“title\u description”:“放开你的味蕾!我们承诺为你提供优质、卫生的美味。”,“img_文件夹”:“image\/caterer\/premium\/”,{“title_no:”1,“title_name:”餐饮”,“title_description:”让您的味蕾放松!我们承诺为您提供美味、优质、卫生的美味。”,“img_文件夹”:“image\/caterer\/标准\/”]
您可以将此更改为

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

但请记住,这将只获取目录的内容,而不是
递归地

要获取URL路径,您必须替换如下路径

str_replace(public_path(), '', $path)
&您还应该应用
资产
功能

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

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

文件系统中的
img\u文件夹在哪里?您也可以使用Laravel,通过使用它提供的功能来拥抱它。看到img\u文件夹是数据库中的列。在哪里链接“image/caterer/luxury/”已写入…此图像文件夹为public/data/image/caterer….$nam=';$ab[]=Storage::allFiles($nam);我需要$nam路径中的所有文件如果您试图使用
$nam
变量作为获取文件的参数,与
glob
示例中的
$name
进行比较,那么会有很大的不同。如果您试图从URL中提取图像列表,那是完全不同的事情,而不是什么
glob
de>Storage::allFiles
就可以了。事实上,我在示例中写错了……这是glob($nam);@SachinMarwha我已经编辑了我的答案,您可以检查一下。
$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