No brainer?

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
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

No brainer?

Post by neophyte »

I figure I'm doing something reallly dumb. This is part of a simple directory class I'm writing. $files returns a populated array when the ''is_file" test is absent. print_r($files); displays what I'd expect it to. When I add "if (is_file($file)){" print_r() print's nothing..... I figure I'm doing something very n00b. Suggestions?

Code: Select all

if ($type =="file"){
		while (($file = readdir($this->dh)) !== false) {
           if (  $file != "." && $file != ".."  ){		
					if (is_file($file)){
						$files[] = $file;
						}
						   }else{
						   $junk[] =$file;
			}
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

You have some missing brackets. Try this:


Code: Select all

<?php
if ($type =="file"){
      while (($file = readdir($this->dh)) !== false) {
           if (  $file != "." && $file != ".."  ){      
               if (is_file($file)){
                  $files[] = $file;
               }
           }else{
               $junk[] =$file;
               
           }// ene elese 
      }// end loop 
}   
?>
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Sorry about that...

Post by neophyte »

Actually here's the class I'm working on. It's not much.

Code: Select all

class dynamicdir {
   var $directory;
   var $path;
   var $dh;
   var $readable;
   var $writable;
   var $executable;
  function dynamicdir($path, $directory){
  	$this->path = $path;
  	$this->directory = $directory;
  		if (is_dir($this->path.$this->directory)) {
  			$this->dh = opendir($this->path.$this->directory);
			}
  		if ($this->dh){
		return $this->dh;
		} else {
		}
  		}//end constructor function


function directorylist($exclude, $type){
if ($type =="file"){
      while (($file = readdir($this->dh)) !== false) {
           if (  $file != "." && $file != ".."  ){      
               if (is_file($file)){
                  $files[] = $file;
               }
           }else{
               $junk[] =$file;
               
           }// ene elese 
      }// end loop 
}    else if  ($type == "dir"){
			while (($file = readdir($this->dh)) !== false) {
           			if ($file != "." && $file != ".."  &&  !is_file($file)){		
						$files[] = $file;
						   }
		}
} else{
		}
return $files;
I'm working on the directorylist().
Here's how I've created the object....

Code: Select all

$dir = new dynamicdir ("/some/absolute/path/","Some_directory");
$files = $dir -> directorylist("", "file");
print_r($files);
the print_r shows a populated array for $files when the is_file or is_dir test is not applied. When I add the is_file/is_dir test print_r does not show anything.
I figure I'm doing something wrong here but I'm not sure what...
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Suggestion: comment your code more.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

neophyte, please please use the

Code: Select all

tags ... 

$file in the function is strictly the filename, not the path.. is_file requires the path.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Sorry

Post by neophyte »

Doh... I knew it was somethin simple. Right on, for now on --

Code: Select all

<?php echo "php tags" ?>
!

Code: Select all

<?php echo "Thanks feyd, Sami, and hawleyjr" 

?>
Post Reply