doucang6739 2012-07-12 17:32
浏览 331
已采纳

从php中的目录中获取所有文件名

(Well what I gone through a lot of posts here on stackoverflow and other sites. I need a simple task, )

I want to provide my user facility to click on upload file from his account, then select a directory and get the list of all the files names inside that directory.

According to the posts here what I got is I have to pre-define the directory name, which I want to avoid.

Is there a simple way to click a directory and get all the files names in an array in PHP? many thanks in advance!

  $dir = isset($_POST['uploadFile']) ? _SERVER['DOCUMENT_ROOT'].'/'.$_POST['uploadFile'] : null;
  if ($_POST['uploadFile'] == true) 
    {
     foreach (glob($dir."/*.mp3") as $filename) {
     echo $filename;
    }
  }
  • 写回答

5条回答 默认 最新

  • dqcd84732 2012-07-12 17:37
    关注

    I will go ahead and post a sample of code I am currently using, with a few changes, although I would normally tell you to look it up on google and try it first.

    if ($handle = opendir($dir)) {
        while (false !== ($file = readdir($handle))) {
            echo $file;
        }
        closedir($handle);
    }
    

    This will display the entire contents of a directory... including: ".", "..", any sub-directories, and any hidden files. I am sure you can figure out a way to hide those if it is not desirable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突