is_dir und is_file funktionieren nicht

NiciB

Erfahrenes Mitglied
Hallo!

Ich hab ein kleines Skript geschrieben, dass einen Ordner auslesen soll und den Inhalt ausgeben mit Unterscheidung zwischen Ordnern und Dateien. (in weiterer Folge soll es alle Dateien und Unterordner mittels Rekursion kopieren)

in meinem Verseichnis (start) habe ich folgende Dateien/Ordner:
Datei: 1.txt
Datei: 2.txt
Datei: 3.txt
Datei: 4.txt
Ordner: unter_ordner

Jetzt zu meinem Skript:

PHP:
<?php

function mycopy($source_dir, $dest){ 
$opendir=opendir($source_dir);

while($content = readdir($opendir)){	
        if(is_file($content) && $content!="." && $content!="..") 
               echo "File: ".$content."<br>";
        if(is_dir($content) && $content!="." && $content!="..")
               echo "Ordner: ".$content."<br>";	
         }
}

mycopy("start", "ziel");
?>

wenn ich jetzt aber die if-Anweisungen weglasse, dann zeigt er den Inhalt an:

PHP:
<?php

function mycopy($source_dir, $dest){ 
$opendir=opendir($source_dir);

while($content = readdir($opendir)){	
        #if(is_file($content) && $content!="." && $content!="..") 
               echo "Inhalt: ".$content."<br>";
        #if(is_dir($content) && $content!="." && $content!="..")
        #       echo "Ordner: ".$content."<br>";	
        }
}

mycopy("start", "ziel");
?>

Ausgabe:
Inhalt: .
Inhalt: ..
Inhalt: 1.txt
Inhalt: 2.txt
Inhalt: 3.txt
Inhalt: 4.txt
Inhalt: unter_start

Wenn ich beide if-Anweisungen verneine, kommt folgende Ausgabe:

PHP:
<?php

function mycopy($source_dir, $dest){ 
$opendir=opendir($source_dir);

while($content = readdir($opendir)){	
        if(!is_file($content) && $content!="." && $content!="..") 
               echo "File: ".$content."<br>";
        if(!is_dir($content) && $content!="." && $content!="..")
               echo "Ordner: ".$content."<br>";	
        }
}

mycopy("start", "ziel");
?>

Ausgabe:

File: 1.txt
Ordner: 1.txt
File: 2.txt
Ordner: 2.txt
File: 3.txt
Ordner: 3.txt
File: 4.txt
Ordner: 4.txt
File: unter_start
Ordner: unter_start


Hab leider keinen Fehler gefunden!
 
Falls sich das Skript nicht im selben Verzeichnis befindet wie $source_dir, müsstest du vor $content noch den Pfad zu $source_dir setzen...sonst kann das Skript nix finden.
 
Zurück