Link to home
Start Free TrialLog in
Avatar of FairyBusiness
FairyBusinessFlag for United States of America

asked on

How to tell php I do not want any directories only files?

In some of my directories I do not only have image files but other directories and I do not want my code to attempt to produce image tags out of them.  So I tried:

if(!is_dir($style)) {
            foreach($style as $img) {
            echo "<img src=\"" . $data . "/" . $img . "\" />";
      }
}

but it still products img tags with the directory names, which obviously are not images.  Anyone know how to tell php to only uses files and not directories?
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Try this:

if(!is_dir($style)) {
            foreach($style as $img) {
              if (!is_dir($img)){
                 echo "<img src=\"" . $data . "/" . $img . "\" />";
              }
      }
}
Avatar of FairyBusiness

ASKER

nope it didnt work.

here is my entire function if it helps:

function woods() {
	global $conn;
	if(isset($_GET['id'])) {
		$id = $_GET['id'];
		$query = "SELECT name FROM submenus WHERE id=" . $id;
		$result = mysql_query($query, $conn);
		confirm_query($result);
		while($rows = mysql_fetch_assoc($result)) {
			$name = $rows['name'];
		}
		$path = "flipbook/" . $name;
		$dirs = scandir($path);
		foreach($dirs as $dir) {
			echo $dir . "<br />";
			$data = $path . "/" . $dir;
			echo $data . "<br />";
			$style = scandir($data);
			//print_r($style[4]);
			if(!is_dir($style)) {
				foreach($style as $img) {
					if (!is_dir($img)){
						echo "<img src=\"" . $data . "/" . $img . "\" />";
					}
				}
			}			
		echo "<br />";
		}
	}
}

Open in new window

Avatar of cfEngineers
cfEngineers


if(!is_dir($style)) {
            foreach($style as $img) {
            $extension = strtolower(substr(strrchr($img, '.'), 1));
            if($extension == 'jpg' || $extension == 'gif' || $extension == 'png'){
                // now use $img as you like
                echo "<img src=\"" . $data . "/" . $img . "\" />";
            } 
      }
}

Open in new window

that made neither the directories nor the image show
ASKER CERTIFIED SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
no that didnt help, it just made the path longer
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
can you post this function

scandir()
its just a function i would online

http://us2.php.net/manual/en/function.scandir.php
Opps, didn't realize.

Try this.

This is similar to a function I used, but mine was more complex.
function woods() {
	global $conn;
	if(isset($_GET['id'])) {
		$id = $_GET['id'];
		$query = "SELECT name FROM submenus WHERE id=" . $id;
		$result = mysql_query($query, $conn);
		confirm_query($result);
		while($rows = mysql_fetch_assoc($result)) {
			$name = $rows['name'];
		}
		$path = "flipbook/" . $name;
		$dirs = opendir($path);
		while ($dir = readdir($dirs)) {
			if ($dir != '.' && $dir != '..') {
				if (!is_dir($path.$dir)) {
					echo "<img src=\"" . $path . $dir . "\" />";
				}
			}
			echo "<br />";
		}
	}
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the function is_dir is just not working for me!!  It isnt reading at all if its a directory or a file.

                  foreach($dirs as $dir => $value) {
                        if(!is_dir($value)) {
                              echo $value . "<br />";
                        }
                  }

I only want it to echo the image file, but its showing all of the directories too. . .
did you try my way with readdir() instead of foreach
thanks