Verzeichnis auslesen

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christoph Weis

    #1

    Verzeichnis auslesen

    Hallo,

    folgendes kleines Skript zeigt leider kein Ergebnis an:

    <?PHP
    function getImagesFromPa th($path)
    {
    $result = array();

    $handle=opendir ($path);

    while ($file = readdir ($handle))
    {
    if ($file != "." && $file != "..")
    {
    if (! is_dir($file))
    {
    $sub = substr($file, -4);
    if ($sub == ".pdf")
    $result[] = $file;
    }
    }
    }

    return $result;
    }

    $fileNames = getImagesFromPa th("./data");
    $num = count($fileName s);

    for ($x=0;$x<$num;$ x++)
    {
    echo "$fileNames[x]<br>";}

    ?>

    Wenn ich aber folgendes verwende : echo "$filename[1]";
    sehe ich den Inhalt von $filename.
    Stimmt an der Schleife etwas nicht?

    Gruß
    Christoph Weis
  • Andy Hassall

    #2
    Re: Verzeichnis auslesen

    On 31 Jan 2004 09:51:13 -0800, c-weis@web.de (Christoph Weis) wrote:
    [color=blue]
    >folgendes kleines Skript zeigt leider kein Ergebnis an:
    >
    ><?PHP
    > function getImagesFromPa th($path)
    > {
    > $result = array();
    >
    > $handle=opendir ($path);[/color]

    You should check if this worked.

    if (!$handle)
    return FALSE; // or whatever is appropriate
    [color=blue]
    > while ($file = readdir ($handle))[/color]

    Should be:

    while (($file = readdir($handle )) !== FALSE)

    Think of what happens if you get a file named "0" ?
    [color=blue]
    > {
    > if ($file != "." && $file != "..")
    > {
    > if (! is_dir($file))
    > {
    > $sub = substr($file, -4);
    > if ($sub == ".pdf")
    > $result[] = $file;
    > }
    > }
    > }
    >
    > return $result;
    > }
    >
    > $fileNames = getImagesFromPa th("./data");
    > $num = count($fileName s);
    >
    > for ($x=0;$x<$num;$ x++)
    > {
    > echo "$fileNames[x]<br>";}[/color]

    That should give a warning if you have error_reporting set high enough. You're
    using 'x' as an array key - not the variable $x.

    Presumably you mean:

    echo $filenames[$x] . '<br>';

    Or:

    echo "{$filename s[$x]}<br>";
    [color=blue]
    >
    > ?>
    >
    >Wenn ich aber folgendes verwende : echo "$filename[1]";
    >sehe ich den Inhalt von $filename.
    >Stimmt an der Schleife etwas nicht?[/color]

    But there isn't a '$filename' variable anywhere in your post. There's
    $fileNames though?

    --
    Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
    <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

    Comment

    Working...