I was trying to populate my dropdown menu with file names from a directory, I have searched for articles here at SO regarding this inquiry I found several answers and tried them but none of them seems to work which I find weird because they were chosen as answers and some of them have High votes take this article for example this is one of the reference that I have tried but it seems like it does not work for me
Here is my code based on the reference link that I Have provided
<select name="templ" class="form-control input-sm">
<?php
foreach(glob(dirname(__FILE__) . '/els-content/*') as $filename){
$filename = basename($filename);
echo "<option value='" . $filename . "'>".$filename. </option>";
}
?>
</select>
this does not return any values at all. What could be the problem ?
Hi Ok so I was able to answer my own question because of what @bobdye had pointed out, okayyy I'll cut to the chase here is how it was solved
dirname(FILE) . '/els-content/*'
returns the current directory plus the string '/els-content/*'
which my problem is the files from els-content does not show on my dropdown so I used echo
to check what does that return so I find out that it returns something like this
Users/WhosPC/Desktop/cms_form/myPadmin/els-content/*
in which els-content
is FOUND OUTSIDE the myPadmin
meaning els-content
is on the same level as my myPadmin
so what I did was I used chdir()
then used getcwd()
inside a variable
so what it look like was
<?php
chdir('../');
$s = getcwd();
?>
<select name="templ" class="form-control input-sm">
<?php
foreach(glob($s . '/els-content/*') as $filename){
$filename = basename($filename);
echo "<option value='" . $filename . "'>".$filename."</option>";
}
?>
</select>
although I'm not sure if chdir('../')
and getcwd()
is reliable enough for this kind of task.. when this cms baby is on the real world up and runnning
feel free to critize this script so I can improve this further
UPDATED CODE
$s = realpath(getcwd()."/..");
foreach(glob($s . '/els-content/*') as $filename){
$filename = basename($filename);
echo "<option value='" . $filename . "'>". $filename."</option>";
}
Ok I didnt delete the 1st version of my code so that users who stumble on this post could see the difference of the 1st one and the 2nd code that @prodigitalson had pointed out on my 1st code snippet