prisonbreaker82 Posted June 5, 2009 Share Posted June 5, 2009 Hi All! I am a newbie of PHP. I tried to solve why I get these two error on my code below. Please help me! Thank you very much! Warning: sort() expects parameter 1 to be array, null given in C:\wamp\www\hello.php on line 13 Warning: Invalid argument supplied for foreach() in C:\wamp\www\hello.php on line 14 ------ <?php echo "<html><head><title>Image Gallery</title></head><body"; $dir = "gallery"; $dh = opendir($dir); while($filename = readdir($dh)) { $filepath = $dir.$filename; if(is_file($filepath) and ereg("\.jpg$",$filename)) { $gallery[] = $filepath; } } sort($gallery); foreach ($gallery as $image) { echo "<hr />"; echo "<img src='$image' /><br>"; } ?> </body></html> Quote Link to comment Share on other sites More sharing options...
ldougherty Posted June 5, 2009 Share Posted June 5, 2009 You are getting the errors because $gallery is not coming back as an array. It probably is not finding any JPG images therefore not assigning the variable. Quote Link to comment Share on other sites More sharing options...
anupamsaha Posted June 5, 2009 Share Posted June 5, 2009 Try like below: <?php echo "<html><head><title>Image Gallery</title></head><body"; $gallery = array(); // Change the following "application" to the exact location of the gallery path that you have $dir = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'application/gallery'; $dh = opendir($dir); while($filename = readdir($dh)) { $filepath = $dir.$filename; if(is_file($filepath) and ereg("\.jpg$",$filename)) { $gallery[] = $filepath; } } sort($gallery); foreach ($gallery as $image) { echo "<hr />"; echo "<img src='$image' /><br>"; } ?> </body></html> Quote Link to comment Share on other sites More sharing options...
prisonbreaker82 Posted June 5, 2009 Author Share Posted June 5, 2009 Thank you for your replies! The code is located in a file called hello.php in the root directory www on my PC.. so I wrote like this $dir = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'gallery' But still same error.. Try like below: <?php echo "<html><head><title>Image Gallery</title></head><body"; $gallery = array(); // Change the following "application" to the exact location of the gallery path that you have $dir = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'application/gallery'; $dh = opendir($dir); while($filename = readdir($dh)) { $filepath = $dir.$filename; if(is_file($filepath) and ereg("\.jpg$",$filename)) { $gallery[] = $filepath; } } sort($gallery); foreach ($gallery as $image) { echo "<hr />"; echo "<img src='$image' /><br>"; } ?> </body></html> Quote Link to comment Share on other sites More sharing options...
kobmat Posted June 5, 2009 Share Posted June 5, 2009 try to change $dir = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'application/gallery'; to $dir = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'application/gallery/'; Quote Link to comment Share on other sites More sharing options...
prisonbreaker82 Posted June 5, 2009 Author Share Posted June 5, 2009 Thank you! Now it did not get an error, but the pictures does not show If I look in the source code, I got a strange \gallery, how can I remove \ before gallery? <html><head><title>Image Gallery</title></head><body><hr /><img src='C:/wamp/www/\gallery/rianobath.jpg' /><br><hr /><img src='C:/wamp/www/\gallery/supermercado1.jpg' /><br><hr /><img src='C:/wamp/www/\gallery/tabela.jpg' /><br><hr /><img src='C:/wamp/www/\gallery/tabela1.jpg' /><br></body></html> try to change $dir = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'application/gallery'; to $dir = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'application/gallery/'; Quote Link to comment Share on other sites More sharing options...
kobmat Posted June 5, 2009 Share Posted June 5, 2009 try this <?php echo "<html><head><title>Image Gallery</title></head><body"; $gallery = array(); // Change the following "application" to the exact location of the gallery path that you have $dir = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'application/gallery'; $dh = opendir($dir); while($filename = readdir($dh)) { $filepath = $dir.$filename; if(is_file($filepath) and ereg("\.jpg$",$filename)) { $gallery[] = 'gallery/'.$filename; } } sort($gallery); foreach ($gallery as $image) { echo "<hr />"; echo "<img src='$image' /><br>"; } ?> </body></html> Quote Link to comment Share on other sites More sharing options...
prisonbreaker82 Posted June 5, 2009 Author Share Posted June 5, 2009 Error again.. MUST be something with my local server settings... It uses \ and / ... why? Warning: opendir(C:/wamp/www/\application/gallery) [function.opendir]: failed to open dir: File exists in C:\wamp\www\hello.php on line 8 Warning: readdir(): supplied argument is not a valid Directory resource in C:\wamp\www\hello.php on line 9 try this <?php echo "<html><head><title>Image Gallery</title></head><body"; $gallery = array(); // Change the following "application" to the exact location of the gallery path that you have $dir = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'application/gallery'; $dh = opendir($dir); while($filename = readdir($dh)) { $filepath = $dir.$filename; if(is_file($filepath) and ereg("\.jpg$",$filename)) { $gallery[] = 'gallery/'.$filename; } } sort($gallery); foreach ($gallery as $image) { echo "<hr />"; echo "<img src='$image' /><br>"; } ?> </body></html> Quote Link to comment Share on other sites More sharing options...
kobmat Posted June 5, 2009 Share Posted June 5, 2009 forgot to make the changes i added on the $dir line <?php echo "<html><head><title>Image Gallery</title></head><body"; $gallery = array(); // Change the following "application" to the exact location of the gallery path that you have $dir = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'application/gallery/'; $dh = opendir($dir); while($filename = readdir($dh)) { $filepath = $dir.$filename; if(is_file($filepath) and ereg("\.jpg$",$filename)) { $gallery[] = 'gallery/'.$filename; } } sort($gallery); foreach ($gallery as $image) { echo "<hr />"; echo "<img src='$image' /><br>"; } ?> </body></html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.