John Doe
John Doe

Reputation: 2924

How to find the Path of the image from the img directory in php?

I am trying to obtain the path for the image in the img directory.

           $keyString = $this->session->userdata('selected_comp_name');
           print($keyString);
           $imageSrcPath = glob('/img/$keyString.{jpeg,gif,png}',GLOB_BRACE);

Here in the above code I am trying to get the company name from session and then I am trying to find the image from the img directory, which has the same name as the company nae but it could have an extension of png,jpeg, or gif. After getting the path I will display it with the img tag. But I am not able to get the path here, I am just getting a blank array.

Any help will be highly appreciated.

My directory structure is www.sitename.com/img/therequiredimage.gif

Upvotes: 0

Views: 81

Answers (1)

Rodion Baskakov
Rodion Baskakov

Reputation: 646

Since there are only three strings of code, I can't see any problems in them. But possible problems could arise if the glob() would return an absolute path of the image (say, /usr/local/www/…/img/therequiredimage.gif) and you couldn't use id with your img tag.

The easiest way in that case would be:

$imageSrcPath = glob('/img/$keyString.{jpeg,gif,png}', GLOB_BRACE);

if(is_array($imageSrcPath) && isset($imageSrcPath[0])) $imgTag = '<img src="/img/'. basename($imageSrcPath[0]) .'"/>';

Upvotes: 1

Related Questions