0
votes

I am working on a wordpress plugin and in that plugin i need to pull all image with .png extention from a folder which reside my plugin images folder. I am using the following code

foreach ( glob( plugin_dir_path(__FILE__ ) .'images/previews/*.png') as $img ) { echo $img.'<br>';
     echo '<img src="$img" alt="">';
    }

when i echo the $img it shows the entire path but when i use this path as the image source it doesnot show the image.It shows a broken image if i dont use the 'alt'. I have searched for it but cant find any solution on web which is working for me. But a raw php file with the same code is working just fine

1
I'm not that familia with PHP, but wouldn't the second echo always echo the following <img src="$img" alt="">? - edhedges

1 Answers

0
votes

If you run this code and look at the source of your page you'll see that you aren't actually putting the value of $img into your img tag.

echo '<img src="$img" alt="">'; // this is what you have now

echo '<img src="' . $img . '" alt="">'; // this is what you need

Edit 10/13/2015 2:52 PM CST: Here is a link to some php code using my above example with the comment this is what you need. It generates a good img tag that if placed in an HTML file and rendered by a browser would show an image on the screen. http://sandbox.onlinephpfunctions.com/code/e7dbe3e475ac83c8eeaa1ac6efb08063a889d6ca

If it's not working make sure you can actually browse (with your browser) to the content inside the src tag.