Question

I recently started working on a PHP File Manager for my server, as I figured it'd be extremely convient to use, as well as allowing me to brush up on my PHP Skills. Anyways, I have a few questions that I hope can be answered...

  • When I list my directorys, there are always a couple of "Dots". For example: ., .., Folder_1, Folder_2, etc... How would I go about removing those "Dots" from my directory list?
  • When I list my directorys, my current method has no problem listing folders with underscores, or ones that have no space in the name. However, it cannot handle Folders with space's in their names. Is there a way to get my File Manager to recognize and handle spaces in the names properly?

Here is my current code...

<?php
global $dir_path;
if (isset($_GET["directory"])) {
    $dir_path = $_GET["directory"];
    //echo $dir_path;
}
else {
    $dir_path = $_SERVER["DOCUMENT_ROOT"]."/";
}

$directories = scandir($dir_path);
foreach($directories as $entry) {
    if(is_dir($dir_path . "/" . $entry )) {
        echo "<a href=?directory=" . $dir_path . "" . $entry . "/" . "><li>" . $entry . "</li></a>";
    }
    else {}
}

?>

Much thanks for any help, Brandon

P.S. Are the "Dots" related to my server's ext4 file-system? It's not really significantly pertinent to my problems, I'm just a tad curious.

Was it helpful?

Solution

If you just want a simple version :

foreach($directories as $entry) {
    if (is_dir($dir_path . "/" . $entry) && !in_array($entry, array('.','..'))) {
        echo "<a href=?directory=" . $dir_path . "" . $entry . "/" . "><li>" . $entry . "</li></a>";
    }
    else {}
}

this checks for . / .. eg current dir and back dir. Regarding the spaces it sounds weird. Is it the link that is not working or is it scandir? If it is the links, replace blanks with %20, eg

$href="?directory=" . $dir_path . "" . str_replace(' ','%20',$entry) . "/";
echo "<a href='".$href.'"><li>' . $entry . '</li></a>';

more likely I think it is the lack of quotes "" around href, eg

echo '<a href="?directory="' . $dir_path . $entry . '/' . '"><li>' . $entry . '</li></a>';

instead. When you are not adding quoutes, a link with blanks, say "test 123" will be interpreted as href=test by the browser, because there is nothing that encapsulates the whole link. It should be href="test 123".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top