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

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

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?