Link to home
Start Free TrialLog in
Avatar of Benji_
Benji_Flag for United Kingdom of Great Britain and Northern Ireland

asked on

PHP List Folders and Sub Folders

hi,

i am trying to write a script where users can upload files via FTP to there designated folders and access them via the web.

i need a way that i can list all files and folders in there home directory via hph so that i can pass the location to a file download script

but im not sure how to get "ALL files and folders listed" in 1 place.


Regars
SOLUTION
Avatar of thinknirmal
thinknirmal
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
scandir will list files and dir in an array; in aditions, you can use readdir and opendir to list depends on your coding

on the other hand, if you just simply want to list content in browser for users to see, just simply take out index file
ASKER CERTIFIED SOLUTION
Avatar of Loganathan Natarajan
Loganathan Natarajan
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you want to do that in a recursive way with PHP5 you could use the SPL classes:
http://php.net/manual/en/recursivedirectoryiterator.construct.php
<?php
 
$directory = '/tmp';
 
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
 
while($it->valid()) {
 
    if (!$it->isDot()) {
        echo 'SubPathName: ' . $it->getSubPathName() . "\n";
        echo 'SubPath:     ' . $it->getSubPath() . "\n";
        echo 'Key:         ' . $it->key() . "\n\n";
    }
 
    $it->next();
}
 
?>

Open in new window

Avatar of Benji_

ASKER

Hi,
Using the code below is there a way you can get if its a file or directory.

Best Regards
<?php
 
$directory = '/tmp';
 
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
 
while($it->valid()) {
 
    if (!$it->isDot()) {
        echo 'SubPathName: ' . $it->getSubPathName() . "\n";
        echo 'SubPath:     ' . $it->getSubPath() . "\n";
        echo 'Key:         ' . $it->key() . "\n\n";
    }
 
    $it->next();
}
 
?>

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Benji_

ASKER

Ok Thanks,

 I canot find how to extract the fileaname from the  strings either.

Best Regards
The filename from a plain string containing the full path?
This will do the basename() function:
http://php.net/manual/en/function.basename.php
Avatar of Benji_

ASKER

Thanks Guys!