0

Possible Duplicate:
Displaying Images from a folder using PHP

This code display all my images from uploadedImages folder, but If I want to display only images from uploadedImages folder that start with letter A, how do I do that?

<?php 
        $files = glob("../uploadedImages/*.*");
        for ($i=0; $i<count($files); $i++)
        {
            $num = $files[$i];
            echo '<img src="'.$num.'" alt="Lunda-Bilder" />'."<br/><br/><br/><br/>";
        }
    ?>
Community
  • 1
  • 1
Toni
  • 97
  • 2
  • 2
  • 7
  • Have you tried `glob("../uploadedImages/a*")`? – feeela Oct 15 '12 at 13:14
  • 3
    Reading the documentation on the `glob()` function will really help when you are using it. – Lix Oct 15 '12 at 13:14
  • @hakre Sorry hakre but my question Its not a duplicate. – Toni Oct 15 '12 at 13:40
  • @Toni: I bet it is. And if not that one, I wonder why the many other existing questions that handle the topic are of not use for you. Users here can only give similar answers to the existing ones, so you might find better answers in searching around. – hakre Oct 15 '12 at 13:42
  • @hakre Yeah thats right Hakre, but if you show me a bit of code explaining how to wright my code It will be better for me. Helping each other (stackoverflow) ... – Toni Oct 15 '12 at 13:49
  • Please keep comments polite and constructive, thanks. – Tim Post Oct 15 '12 at 14:02

2 Answers2

3

As simply as

$files = glob("../uploadedImages/A*.*");

If you'd like to refine it a bit more so you get images returned, you can use

$files = glob("../uploadedImages/[aA]*.{jpg,png,gif,bmp}", GLOB_BRACE);
Havelock
  • 6,913
  • 4
  • 34
  • 42
  • 2
    +1 I prefer this solution as you are then only working with an array of files that are relevant - although the difference in performance for a sensible directory of images would be hard to notice. You could change to `A*.png`, for example, to filter types. Documentation: http://php.net/manual/en/function.glob.php – Fenton Oct 15 '12 at 13:16
  • @Havelock Your code above Its not working. – Toni Oct 15 '12 at 13:35
  • 1
    @Toni please specify "Not working". – Havelock Oct 15 '12 at 13:50
  • @Havelock I have tried your first line above, and my page is displaying blank. I have tried the second line above, and my page is displaying blank. – Toni Oct 15 '12 at 13:54
  • 1
    @Toni: [PHP's white screen of death](http://stackoverflow.com/questions/1475297/phps-white-screen-of-death) and also this might be of help: [What does this error mean in PHP? - Nothing is seen. The page is empty and white.](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12772851#12772851) – hakre Oct 15 '12 at 14:05
  • @Toni, is you remove that line of (my) code and run your script, is it still ok? – Havelock Oct 15 '12 at 14:10
  • @Havelock If I remove your line of code, and I run my code like my question code, Its still showing everything perfect. – Toni Oct 15 '12 at 14:23
  • You could try adding `error_reporting( E_ALL );` at the beginning of your script and then run it again. This should give you an idea, where the error is. Also check the links @hakre posted in his comment above. – Havelock Oct 15 '12 at 14:28
  • 1
    @Havelock Ok, the problem is solved. I tried this code and Its working $files = glob("uploadedImages/*c*");. If I change the letter that I want between ** is calling/displaying all the images that start with the letter you put between **. – Toni Oct 15 '12 at 14:31
  • How about other files that contain `c` in their names, without it (the `c`) being the first letter? – Havelock Oct 15 '12 at 14:33
  • @Havelock Its displaying every image word that have a c in their names, if I have *c*. That is not what I wanted. So I have to figure that out now. – Toni Oct 15 '12 at 14:43
  • @Toni I've updated the second line in my answer, there was one comma too many. Try now. – Havelock Oct 15 '12 at 14:52
  • @Toni, that's good it's now working. It was the second comma, which neither I nor anyone else seem to have spotted. – Havelock Oct 15 '12 at 17:43
0
<?php 
$files = glob("../uploadedImages/{A,a}*.*", GLOB_BRACE);
for ($i=0; $i<count($files); $i++)
{
    $num = $files[$i];
    echo '<img src="'.$num.'" alt="Lunda-Bilder" />'."<br/><br/><br/><br/>";
}
?>
Mihail Burduja
  • 3,196
  • 2
  • 22
  • 28