4
votes

PHP versione 5.2.*

my function not working :/

images in server, in folder: /public_html/gallery/images

images in server

<?php
    foreach(glob('gallery/images/*', GLOB_NOSORT) as $image)   
    {  
        echo "Filename: " . $image . "<br />";      
    }  
?>

any help? what im doing wrong?

error i get is : Warning: Invalid argument supplied for foreach() in /home/a9773555/public_html/gallery/index.php on line 2

1
What does var_dump(getcwd()); show? - DaveRandom
There's nothing wrong with your code. What are you trying to achieve? My guess is that you're running this outside your root. Place that file in the root of your system and try it again, if that's the case. - Funk Forty Niner
Seems like glob('gallyer/images/*', GLOB_NOSORT) is not returning an array. Probably encountered an error and returns a false, which is an invalid argument for foreach. - Patrick Manser
well it was because of i have index.php in gallery folder and i dont have galley folder in gallery folder :D im working in gallery folder so i just needed "images/*" - Markus

1 Answers

7
votes

The problem looks you have put your php file in gallery folder...

/home/a9773555/public_html/gallery/index.php on line 2

if that is the case (if you put index.php in gallery) then try the following:

<?php
    foreach(glob('images/*', GLOB_NOSORT) as $image)   
    {  
        echo "Filename: " . $image . "<br />";      
    }  
?>

Or do the following,

Put your index.php in your /home folder. then...

<?php
    foreach(glob('a9773555/public_html/gallery/images/*', GLOB_NOSORT) as $image)   
    {  
        echo "Filename: " . $image . "<br />";      
    }  
?>

Give it a try. and let me know...