Reputation: 137
Database have link of folder and folder contain many images. I want to get all images name but glob() (function) is not working..
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);
}
output
<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=3)</i>
0 <font color='#888a85'>=></font>
<b>array</b> <i>(size=0)</i>
<i><font color='#888a85'>empty</font></i>
1 <font color='#888a85'>=></font>
<b>array</b> <i>(size=0)</i>
<i><font color='#888a85'>empty</font></i>
2 <font color='#888a85'>=></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\/"}]
Upvotes: 1
Views: 1334
Reputation: 5176
You can change this to
foreach ($users as $user) {
$name = $user->img_folder;
$nam = 'http://127.0.0.1/shaadi/public/data/'.$name;
$ab[] = glob($name);
}
this
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
}
But remember this will only get contents of a directory not
recursively
.
To get the URL path you have to str_replace
the paths like
str_replace(public_path(), '', $path)
& also you should apply the asset
function
asset(str_replace(public_path(), '', $path))
Now if you are returning $ab
variable which contains all the paths in a multidimensional array then you can either loop through or create a recursive function to achieve this or you can do it when setting value in $ab
like this
$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
This is a modified sample code from my project which worked for me although I didn't tested for your purpose but it should work.
Upvotes: 1