Search for files beginning in "t_"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
4fit?
Forum Newbie
Posts: 6
Joined: Sat Oct 04, 2008 9:37 am
Location: Graham, NC

Search for files beginning in "t_"

Post by 4fit? »

I am using the code below to cycle through all files and subfolders within a particular directory. The results are placed in an array and then echo'd out. What I need to do is tweak this code so that it filters the items added to the list to only those whose filename begins with "t_". I then need the link the image to the matching filename in that same location that does not begin with "t_".

Any help is greatly appreciated! Thanks in advance!

Code: Select all

 
<?php
 
    // files to search for
    $extList = array();
        $extList['gif'] = 'image/gif';
        $extList['jpg'] = 'image/jpeg';
        $extList['jpeg'] = 'image/jpeg';
        $extList['png'] = 'image/png';
 
    // set to specific image if needed
    // null will result in a random image
    $img = null;
    
    // path to the directory you want to scan
    $directory = 'images/';
    if (substr($directory,-1) != '/') {
        $directory = $directory.'/';
    }
 
    // if img is set, show it
    if (isset($_GET['img'])) {
        $imageInfo = pathinfo($_GET['img']);
        if (isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
            file_exists( $directory.$imageInfo['basename'] )) {
            $img = $directory.$imageInfo['basename'];
        }
    } 
    // if img isnt set, grab a random
    else {
        //cycle through directory and subfolders
        $it = new RecursiveDirectoryIterator("$directory");
        foreach(new RecursiveIteratorIterator($it) as $file)
        {
            $file_info = pathinfo($file);
            if (isset( $extList[ strtolower( $file_info['extension'] ) ] ) &&
                strpos($file_info['basename'],"03")) {
                $items[] = $file;
            }
        }
        sort($items);
        // get the random image
        if (count($items) > 0) {
            $imageNumber = rand(0,count($items));
            //$imageNumber = time() % count($items);
            //$img = $directory.$items[$imageNumber];
            $img = $items[$imageNumber];
        }
    }
 
    // if img isnt null, display it
    if ($img!=null) {
        $imageInfo = pathinfo($img);
        //$contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
        //header ($contentType);
        //readfile($img);
        echo '<a href="' . $img . '"><img src="' . $img . '" alt="" /></a>';
    } 
    // else, try to create one or give error
    else {
        /*if ( function_exists('imagecreate') ) {
            //header ("Content-type: image/png");
            $im = @imagecreate (100, 100)
                or die ("Cannot initialize new GD image stream");
            $background_color = imagecolorallocate ($im, 255, 255, 255);
            $text_color = imagecolorallocate ($im, 0,0,0);
            imagestring ($im, 2, 5, 5,  "IMAGE ERROR", $text_color);
            imagepng ($im);
            imagedestroy($im);
        }*/
        die("Unable to get image");
    }
?>
 
phppucci
Forum Newbie
Posts: 17
Joined: Fri Oct 03, 2008 2:27 pm

Re: Search for files beginning in "t_"

Post by phppucci »

if(substr($filename, 0, 2) == 't_') {
// add to list
}
4fit?
Forum Newbie
Posts: 6
Joined: Sat Oct 04, 2008 9:37 am
Location: Graham, NC

Re: Search for files beginning in "t_"

Post by 4fit? »

phppucci wrote:if(substr($filename, 0, 2) == 't_') {
// add to list
}
Thanks for the reply. I added that to the file, but not I get the below error.
"Warning: sort() expects parameter 1 to be array, null given in /home/musta2/public_html/images/rotator_2.php on line 40
Unable to get image"

Here is the code portion that adds it to the array. Any guidance provided would be great.

Code: Select all

 
        $it = new RecursiveDirectoryIterator("$directory");
        foreach(new RecursiveIteratorIterator($it) as $file)
        {
            $file_info = pathinfo($file);
            if (isset( $extList[ strtolower( $file_info['extension'] ) ] ) &&
                substr($filename, 0, 2) == 't_') {
                $items[] = $file;
            }
        }
        sort($items);
 
phppucci
Forum Newbie
Posts: 17
Joined: Fri Oct 03, 2008 2:27 pm

Re: Search for files beginning in "t_"

Post by phppucci »

4fit, $filename is only symbolic name of my variable :)
if real file name is stored in other variable use that instead
of $filename in substr function.
4fit?
Forum Newbie
Posts: 6
Joined: Sat Oct 04, 2008 9:37 am
Location: Graham, NC

Re: Search for files beginning in "t_"

Post by 4fit? »

phppucci wrote:4fit, $filename is only symbolic name of my variable :)
if real file name is stored in other variable use that instead
of $filename in substr function.
d'oh! Thought I had changed that. Pretty sad that I am a VB programmer by trade and I overlooked a variable name. :lol:

Alright, I got it working. However, every so often I get the "Unable to get image" message that my die function creates. Any ideas why? For testing purposes, there are only a handful of images in my directories whose filename begins with "t_". But, even if there is only one in the directory, I would expect it to show that one image over and over again. The only time that it should hit the die function is if there are absolutely no filenames that begin with "t_".

Here is my test page

Any ideas? Thanks!

Code: Select all

<?php
 
    // files to search for
    $extList = array();
        $extList['gif'] = 'image/gif';
        $extList['jpg'] = 'image/jpeg';
        $extList['jpeg'] = 'image/jpeg';
        $extList['png'] = 'image/png';
 
    // set to specific image if needed
    // null will result in a random image
    $img = null;
    
    // path to the directory you want to scan
    $directory = 'images/';
    if (substr($directory,-1) != '/') {
        $directory = $directory.'/';
    }
 
    // if img is set, show it
    if (isset($_GET['img'])) {
        $imageInfo = pathinfo($_GET['img']);
        if (isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
            file_exists( $directory.$imageInfo['basename'] )) {
            $img = $directory.$imageInfo['basename'];
        }
    } 
    // if img isnt set, grab a random
    else {
        //cycle through directory and subfolders
        $it = new RecursiveDirectoryIterator("$directory");
        foreach(new RecursiveIteratorIterator($it) as $file)
        {
            $file_info = pathinfo($file);
            if (isset( $extList[ strtolower( $file_info['extension'] ) ] ) &&
                substr($file_info['basename'], 0, 2) == "t_") {
                $items[] = $file;
            }
        }
        sort($items);
        // get the random image
        if (count($items) > 0) {
            $imageNumber = rand(0,count($items));
            $img = $items[$imageNumber];
        }
    }
 
    // if img isnt null, display it
    if ($img!=null) {
        $imageInfo = pathinfo($img);
        echo '<a href="' . $img . '"><img src="' . $img . '" alt="" /></a>';
    } 
    // else, die
    else {
        die("Unable to get image");
    }
?>
phppucci
Forum Newbie
Posts: 17
Joined: Fri Oct 03, 2008 2:27 pm

Re: Search for files beginning in "t_"

Post by phppucci »

Code: Select all

 
// if you have 5 items in array index of last item is 4 not 5 :)
if(count($items) > 0) {
    $imageNumber = rand(0,count($items) [color=#FF0000]- 1[/color]);
    $img = $items[$imageNumber];
}
4fit?
Forum Newbie
Posts: 6
Joined: Sat Oct 04, 2008 9:37 am
Location: Graham, NC

Re: Search for files beginning in "t_"

Post by 4fit? »

phppucci wrote:

Code: Select all

 
// if you have 5 items in array index of last item is 4 not 5 :)
if(count($items) > 0) {
    $imageNumber = rand(0,count($items) [color=#FF0000]- 1[/color]);
    $img = $items[$imageNumber];
}
Dang it! I knew that too! :crazy: Thanks for the help bro. PHP is brand new to me, but appears to be a fun language to learn.
phppucci
Forum Newbie
Posts: 17
Joined: Fri Oct 03, 2008 2:27 pm

Re: Search for files beginning in "t_"

Post by phppucci »

Hehe, np :)
Post Reply