Reputation: 2051
Can anyone tell me what's wrong with this code? It is supposed to display all folders in ../files/
, however, it is returning an empty list. This is the code:
<?php
$upload_dir = chdir("./files/" . $userid);
$dirs = glob($upload_dir, GLOB_ONLYDIR);
foreach($dirs as $val){
echo '<option value="'.$val.'">'.$val."</option>\n";
}
?>
Upvotes: 1
Views: 1252
Reputation: 9311
chdir()
returns true
or false
depending on whether changing the directory worked. So you basically try to find all files named either 1
or 0
. If there is any clearly defined behaviour of this at all.
Try it like this:
<?php
if (chdir("./files/" . $userid)) {
$dirs = glob('*', GLOB_ONLYDIR);
foreach($dirs as $val){
echo '<option value="'.$val.'">'.$val."</option>\n";
}
} else {
echo 'Changing directory failed.';
}
?>
Upvotes: 2
Reputation: 6356
You've got $upload_dir
set to the return from chdir
, which will be a TRUE or a FALSE, and then try to use that as your argument to glob when settings $dirs.
The first argument to glob should be a string, representing a pattern. In your case, you'll probably want * to list all contents (instead of the TRUE or FALSE you're passing in). Think of the first argument to glob as what you would pas to dir
on Windows, or to ls
in Linux, so in this case /files/userid/*
or something similar.
I'd do something like this instead:
<?php
$pattern = "./files/" . $userid . '/*';
$dirs = glob($pattern, GLOB_ONLYDIR);
foreach($dirs as $val){
echo '<option value="'.$val.'">'.$val."</option>\n";
}
?>
This has the advantage of not changing the current working directory. Ideally, I'd use an absolute path instead of the relative path there.
Upvotes: 0