Autor Beitrag
Gerhard_S
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 98



BeitragVerfasst: Mi 14.07.10 00:57 
Hallo,
wie finde ich mit php heraus, ob ein Verzeichnis ein Unterverzeichnis hat? Der Name des Unterverzeichnisses ist unbekannt.
Ich habe es versucht mit
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
<?php
error_reporting(E_ALL);
$filename = '.';
if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}
?>

aber das liefert mir immer ein positives Ergebnis, auch wenn gar kein Unterverzeichnis existiert.
Was nun?


Moderiert von user profile iconChristian S.: Topic aus Delphi4PHP-Technologien verschoben am So 15.07.2012 um 13:17
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mi 14.07.10 01:44 
scan_dir und is_dir ... HTH.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Flamefire
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1207
Erhaltene Danke: 31

Win 10
Delphi 2009 Pro, C++ (Visual Studio)
BeitragVerfasst: Mi 14.07.10 11:55 
BTW: "." ist der alias für das aktuelle verzeichnis. Das existiert logischerweise immer.
Gerhard_S Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 98



BeitragVerfasst: Mi 14.07.10 13:32 
Den langen Weg habe ich durch mehrstündiges Herumprobieren selbst herausgefunden:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
<?php
$dir = dirname(__FILE__);
$files1 = scandir($dir.'../');
$count="0";
   for($i=0; $i < count($files1); $i++) {
      if ((is_dir($files1[$i])) and ($files1[$i] != '.') and ($files1[$i] != '..'))
       {
         echo "UVZ namens: ".$files1[$i]." gefunden.";
         $count=$count+1;
        }
   }
echo "Es gibt ".$count." Unterverzeichnisse.";
?>

Vielleicht kennt jemand doch eine Funktion, die das ganze auf ein oder zwei Zeilen Code reduziert?
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mi 14.07.10 14:38 
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
<?php
$dir = dirname(__FILE__);
$files = scandir($dir.'../');

$dirs = array();
array_walk($files, function($v, $k, $u) {
    if(is_dir($u.$v) && !in_array($v, array('.''..'))) {
        $dirs[] = $v;
    }
}, $dir);
$count=count($dirs);

echo "Es gibt ".$count." Unterverzeichnisse.";
?>

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Heiko
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Do 15.07.10 21:43 
Oder verständlicher:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
<?php
$dir = dirname(__FILE__);
$files = scandir($dir.'../');

$dirs = array();
foreach ($files as $file) {
  if (is_dir($file) && !in_array($file, array('.''..'))){
    $dirs[] = $file
  }
}

echo "Es gibt ".count($dirs)." Unterverzeichnisse.";
?>


PS: liefert dirname nicht eigehntlich einen Ordner ohne "/" am Ende zurück?