-2

i'm trying to get all the files and folders in a directory using PHP.

at the moment, I can only get the folders using my code.

this is my current code:

<?php 
$folders = "";
if(isset($_GET['dir'])) {
    $directory = $_GET['dir'];
$path = 'user_doc/'.$directory.''; // '.' for current
foreach (new DirectoryIterator($path) as $file) {
    if ($file->isDot()) continue;

    if ($file->isDir()) {
        //print $file->getFilename() . '<br />';
        //$folders .="<option>".$file->getFilename()."</option>";

        $folders .="<div style=' width:90%; padding:14px; border-bottom:solid 1px #CCC;'><i style='font-size:18px;' class='fa fa-folder-open'></i>&nbsp;
<a class='defaults'  href='folder.php?dir=".$file->getFilename()."'>".$file->getFilename()."</a></div>";
    }
}
}
?>

the code above, ignores the files for some reason and it will only display the folders.

could someone please advise on this issue?

Thanks in advance.

TERO
  • 159
  • 2
  • 16
  • 1
    is this a trick question? your string markup is inside the if block with `->isDir()` method, what do you expect – Kevin Mar 24 '15 at 11:39
  • First of all: You are building yourself a security nightmare by allowing arbitrary paths to be passed. Also; you are checking for `isDir` and not handle a file? – Rangad Mar 24 '15 at 11:40
  • @Rangad, this system is for company's internal use. so no normal users will have access to it. and for that reason, there is no "security nightmare" . – TERO Mar 24 '15 at 11:42
  • So the angry, recently laid off employee scenario isn't going to happen? Or that someone else later works on the code and uses the value for something more dangerous than listing directories? The "only for internal use" mentality made bigger companies than yours suffer. But that's not my business. – Rangad Mar 24 '15 at 11:48
  • @Rangad, my boss is a web developer and he's reading your comments right now as I'm typing this message and he's totally disagreeing with you. 1st, like i said, this is only for internal use so no malicious user will have access to it. 2nd, no one apart from myself will edit this code in the future, 3rd, I would love if you could name a few companies that "suffered" because of internal use mentality! unless you just come up with your statistics as you go along! – TERO Mar 24 '15 at 11:52
  • No need to be agressive. I just wanted to point out to you that the code (if it's ever exposed to a 3rd party) is (in the form posted above) vulnerable, as you are passing unvalidated user data to a file-sytem function. Which allows one to list the contents of any directory the executing process has access to. (A mitigated `directory traversal attack`; google has enough examples for that). – Rangad Mar 24 '15 at 12:52
  • @Rangad, i didn't even read the rest of your comment as soon as I read the first bit (No need to be aggressive.). I am not being aggressive whatsoever! you said your point and I said mine. lets just agree to disagree and leave it to that. – TERO Mar 24 '15 at 13:39

3 Answers3

3

This should work for you:

(Here I just use glob() to get all files and directory's)

<?php

    if(isset($_GET['dir'])) {
        $directory = $_GET['dir'];
        $path = "user_doc/" . $directory;
        $folders = "";
        $files = glob($path . "/*");

        foreach($files as $file)
            $folders .= "<div style=' width:90%; padding:14px; border-bottom:solid 1px #CCC;'><i style='font-size:18px;' class='fa fa-folder-open'></i>&nbsp;<a class='defaults'  href='folder.php?dir=" . $file . "'>" . $file . "</a></div>";
    }

?>

And your code didn't worked as you wanted, because of this:

if ($file->isDir()) {

You only entered the if statement if it is a directory

Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • worked like a charm. this is the first time that I am working with files and folders using PHP. – TERO Mar 24 '15 at 11:43
  • Thanks buddy. appreciate it mainly because you just answered the question without sarcasm unlike many people on here :)./.. some people think everyone's born with PHP knowledge. lol# – TERO Mar 24 '15 at 11:46
2

Read it loud and listen:

if ($file->isDir()) {
Nat
  • 323
  • 2
  • 6
0

Check this answer given by Shef for listing all the folder and files by

function listFolderFiles($dir){
    $ffs = scandir($dir);
    echo '<ol>';
    foreach($ffs as $ff){
        if($ff != '.' && $ff != '..'){
            echo '<li>'.$ff;
            if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
            echo '</li>';
        }
    }
    echo '</ol>';
}

listFolderFiles('Main Dir');
Community
  • 1
  • 1
Ghostman
  • 6,042
  • 9
  • 34
  • 53