redditor
redditor

Reputation: 4286

PHP Directory Contents - Count error & strip certain files

I currently have the following code for a no thrills uploader. It also displays the contents of the containing folder, however echo "$indexCount files<br/>"; returns the number of files + 2 but I cannot see how the variable arrives at this number.

Additionally I would like the table to not display the page itself (index.php) and the error_log. I attempted something along the lines of this logic: if the name of the file is x or y then go to next file but I could not get it to work.

Thanks.

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Please choose a file: <input name="file" type="file" /><br />
<input type="submit" value="Upload" /></form>


<?php 

if (!empty($_FILES["file"]))
{
    if ($_FILES["file"]["error"] > 0)
       {echo "Error: " . $_FILES["file"]["error"] . "<br>";}
    else
       {echo "Stored file:".$_FILES["file"]["name"]."<br/>Size:".($_FILES["file"]["size"]/1024)." kB<br/>";
       move_uploaded_file($_FILES["file"]["tmp_name"],$_FILES["file"]["name"]);
       }
}

    // open this directory 
    $myDirectory = opendir(".");
    // get each entry
    while($entryName = readdir($myDirectory)) {$dirArray[] = $entryName;} closedir($myDirectory);
    $indexCount = count($dirArray);
        echo "$indexCount files<br/>";
    sort($dirArray);

    echo "<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks><TR><TH>Filename</TH><th>URL</th><th>Wiki Syntax</th></TR>\n";

        for($index=0; $index < $indexCount; $index++) 
        {
            if (substr("$dirArray[$index]", 0, 1) != ".")
            {
            echo "<TR>
            <td><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>
            <td>http://www.example.com/$dirArray[$index]</td>
            <td><input type='text' value='[[http://www.example.com/$dirArray[$index]|$dirArray[$index]]' /></td>
                </TR>";
            }
        }
    echo "</TABLE>";
    ?>

Upvotes: 0

Views: 131

Answers (2)

The reason why it returns number of files + 2 is probably the readdir call count also the . and .. directory entries, which refer to both the current and the parent directory.

Upvotes: 1

pamil
pamil

Reputation: 980

They are . and .. directories. You can not see them because substr("$dirArray[$index]", 0, 1) != "." returns false for them.

In while loop you can filter them, or you can just unset first two elements of $dirArray.

Upvotes: 1

Related Questions