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!
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!
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.
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.
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_".
<?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");
}
?>
// 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];
}
// 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! Thanks for the help bro. PHP is brand new to me, but appears to be a fun language to learn.