Page 1 of 1

Picture Gallary funktion

Posted: Sun May 11, 2008 11:49 am
by Enjoiskater
Hello

I´m pretty new when it comes to PHP and I´m trying to learn and right know I´m trying to build a new picture gallery to my site. I have gotten this code from a friend to help me get started but I´m stuck right know.

Code: Select all

<?php
/**
* Returns an array with the paths of all images in the directory $dir.
* If $fullDir is set to true, subdirectives and their images will be
* returned as well, as arrays withing the array.
*
* @param $dir The directive in which to look for images
* @param $fullDir Decide weather or not to look in subdirectives as well
*
* @return An array holding image paths and possibly subarrays holding
           paths of images in subdirectives
*
*/
function getImagesInDir($dir = 'Bilder/Bildserier/Naruto Ultimate Ninja Storm', $fullDir = true)
{      
    $allowedExtensions = array('jpg', 'gif', 'png');
    $allImages = array();
    
    // Om $dir är ett giltligt direktiv
    if(is_dir($dir)) {
        if($dh = opendir($dir)) {
            // Medan det finns fler filer, fortsätt att läsa in dem
            while(($file = readdir($dh)) !== false) {
                // Ignorera s.k. 'punkt-filer'
                if($file != "." && $file != "..") {
                    // Om denna fil är en katalog (mapp)
                    if(filetype($file) == 'dir') {
                        $tmp = getImagesInDir($dir.$file, $fullDir);
                        if (!empty($tmp)) {
                            $allImages[] = $tmp;
                        }
                    } elseif(in_array(getExtension($file), $allowedExtensions)) {
                        $allImages[] = $dir.$file;
                    } // if
                } // if
            } // while
        } // if
    } // if
    return $allImages;
}
 
/**
* Returns the file extension of a file
*
* @param $imageName The complete image name
*
* @return The file's extension, excluding a dot
*/
function getExtension($imageName)
{
    return end(explode('.', $imageName));
}
?>
 
 
Thats the code but I cant get PHP to write out the pictures. I have tried some diffrent ways but it dosen´t work at all and I´m hoping that maybe someone can help me to get the code to show me the pictures.

Re: Picture Gallary funktion

Posted: Sun May 11, 2008 12:45 pm
by JAB Creations
I'm looking at the output which is totally blank so I was wondering why and then presumed that you're loading images from a different source.

Then I still see no HTML output...or anything...

So I looked through your code and there is nothing being echoed.

So I attempted to echo nearly all of your variables.

...so would it be safe to presume this is not all of the PHP you're working with?

Re: Picture Gallary funktion

Posted: Sun May 11, 2008 12:48 pm
by JAB Creations
Haha I just realized the function wasn't being called. I've been working with functions for so long in JavaScript, working with PHP for a few years now, and I have just recently started using functions in PHP. :|

Ok so I executed the function at the end of the page and there are some errors...

Code: Select all

getImagesInDir();
Functions are like taxis, they won't show up unless you call for them.

Re: Picture Gallary funktion

Posted: Sun May 11, 2008 12:51 pm
by Enjoiskater
Hello, thanks for the answer. But that is what I´m having problems with. I cant call the function, I dont know how write to call it because I have tried some diffrent ways but with no luck. So what I really would like help with is to actully call the function and make the code work.

Re: Picture Gallary funktion

Posted: Sun May 11, 2008 1:03 pm
by JAB Creations
Did you read my last post?

Re: Picture Gallary funktion

Posted: Sun May 11, 2008 1:10 pm
by Enjoiskater
JAB Creations wrote:Did you read my last post?
Sorry, I must have jumped over the post. Thanks I gonna try it and see what there are for som errors.