Jump to content

Image Gallery help


Recommended Posts

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>

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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/';

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.