doufei2355 2015-08-09 15:19
浏览 26
已采纳

目录中的图像文件[关闭]

firstly I'd like to tell you that I know there are some answers already for this problem, but I don't know PHP, so I'd be really glad if you could help me.

So I have my website directory divided like that

  1. css
  2. images
    1. abandonedhotel
    2. katka
  3. js
  4. php

Well, what I'd like to know if there's possibility for .php file in "php" folter to access the "images" folder and subfolders and count the .jpg files there. Also if there could be some exclusion for "title.jpg" because I don't need that to count in. Also if is it possible to list those .jpg files (again without title.jgp) that'd be cool.

Thanks in advance to anybody who helps me.

  • 写回答

1条回答 默认 最新

  • dongpo2340 2015-08-09 18:15
    关注

    Could be something around the lines of:

    <?php
    
    function endsWith($haystack, $needle) {
        // search forward starting from end minus needle length characters
        return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== FALSE);
    }
    
    function rsearch($folder, $pattern) {
        $dir = new RecursiveDirectoryIterator($folder);
        $ite = new RecursiveIteratorIterator($dir);
        $files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH);
        $fileList = array();
        foreach($files as $file) {
            $fileList = array_merge($fileList, $file);
        }
        return $fileList;
    }
    
    $count = 0;
    foreach(rsearch("../images/", "_.*jpg_iU") as $file) {
      if(!endsWith($file, "title.jpg")) {
        $count++;
        // output $file
      }
    }
    
    ?>
    

    -> php glob - scan in subfolders for a file

    -> startsWith() and endsWith() functions in PHP

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?