dsiuz86842 2013-05-08 09:41
浏览 12
已采纳

PHP上的文件名读取

Im trying to read filenames from a directory using the code and adding a filter to readonly files with the current year and month in the filename for example

Julius Robles_Client11_20130508_10-42-42_AM.zip
Julius Robles_Client12_20130508_11-45-42_AM.zip
Julius Robles_Client13_20130508_11-58-42_AM.zip

so the code will only return files with 201305 in their names but it returns a correct filtered set but some files are missing and i dont know why?

also what is the file "." and ".." stored in the first 2 rows of the array?

heres the code

$filenames = array();
if ($handle = opendir('archive/search_logs/')) {
  $ctr = 0;
  while (false !== ($entry = readdir($handle))) {
    //if(strpos($entry,date('Ym')) !== false){
    $name = $entry;
    $entry = str_replace("-",":",$entry);
    $filenames[$ctr] = explode("_", $entry);
    $filenames[$ctr][] = $name;
    $ctr++;
  //}
}
  closedir($handle);
}
  • 写回答

2条回答 默认 最新

  • duangekui7451 2013-05-08 10:02
    关注

    why scandir? Use DirectoryIterator don't be affraid to use modern PHP

    from manual:

    The DirectoryIterator class provides a simple interface for viewing the contents of filesystem directories.

    example:

    <?php
    $filenames = array();
    $iterator = new DirectoryIterator($directory);
    foreach ($iterator as $fileinfo) {
        if ($fileinfo->isFile()) {
            $filenames[] = $fileinfo->getFilename();
        }
    }
    print_r($filenames);
    
    ?>
    

    with DirectoryIterator you can check $fileInfo via this methods:

    • DirectoryIterator::isDir — Determine if current DirectoryIterator item is a directory
    • DirectoryIterator::isDot — Determine if current DirectoryIterator item is '.' or '..'
    • DirectoryIterator::isExecutable — Determine if current DirectoryIterator item is executable
    • DirectoryIterator::isFile — Determine if current DirectoryIterator item is a regular file
    • DirectoryIterator::isLink — Determine if current DirectoryIterator item is a symbolic link
    • DirectoryIterator::isReadable — Determine if current DirectoryIterator item can be read
    • DirectoryIterator::isWritable — Determine if current DirectoryIterator item can be written
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: