Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try changing the echo line in the function to this:</p> <pre><code>echo '&lt;img src="images/' . $file . '" /&gt;' ; </code></pre> <p>Hope this will help :)</p> <p><strong>OR</strong></p> <p>Outside the function, use it like this:</p> <pre><code>$images = returnimages(); //will get the array containing the images foreach($images as $img) { echo '&lt;img src="images/' . $img . '" /&gt;'; } </code></pre> <p>Also, you have to include this line inside the if condition in the while loop:</p> <pre><code>$files[] = $file; //insert the file into the array. This array is what we are going to return from the function </code></pre> <p><strong>Update</strong></p> <p>I have tested this code and it's working:</p> <pre><code> //PHP SCRIPT: getimages.php header('content-type: application/x-javascript'); function returnimages($dirname="./images") { $pattern="([^\s]+(\.(?i)(jpg|png|gif|bmp))$)"; // http://www.mkyong.com/regular-expressions/how-to-validate-image-file-extension-with-regular-expression/ $files = array(); if($handle = opendir($dirname)) { while(false !== ($file = readdir($handle))){ if(preg_match($pattern, $file)){ //if this file is a valid image $files[] = $file; } } closedir($handle); } //sort($files); // http://php.net/manual/en/function.sort.php natcasesort($files); // case insensitive "natural order" algorithm :: http://php.net/manual/en/function.natcasesort.php return($files); } $images = returnimages(); //will get the array containing the images foreach($images as $img) { echo '&lt;img src="images/' . $img . '" /&gt;'; } </code></pre> <p>In my <code>images</code> folder, I have put 5 files for testing. And upon running it, it would give the following:</p> <pre><code>&lt;img src="images/a.jpg" /&gt; &lt;img src="images/ba.jpg" /&gt; &lt;img src="images/ca.jpg" /&gt; &lt;img src="images/Copy (3) of a.jpg" /&gt; &lt;img src="images/Copy (4) of a.jpg" /&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload