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
https://forums.phpfreaks.com/topic/161065-image-gallery-help/
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
https://forums.phpfreaks.com/topic/161065-image-gallery-help/#findComment-849970
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
https://forums.phpfreaks.com/topic/161065-image-gallery-help/#findComment-849996
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
https://forums.phpfreaks.com/topic/161065-image-gallery-help/#findComment-850041
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
https://forums.phpfreaks.com/topic/161065-image-gallery-help/#findComment-850046
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
https://forums.phpfreaks.com/topic/161065-image-gallery-help/#findComment-850049
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
https://forums.phpfreaks.com/topic/161065-image-gallery-help/#findComment-850052
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.