douchen2011 2015-03-06 00:46
浏览 33
已采纳

使用目录中的文件名填充下拉列表在PHP中

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 ?

  • 写回答

1条回答 默认 最新

  • duanlaiyin2356 2015-03-06 01:40
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀