doudu4282 2018-02-28 02:31
浏览 16
已采纳

检查具有相似名称的多个文件

I have checked many questions about "if file or files exist", but my issue is that I would love to generalise this part of the code by asking if files (in my case pictures) with specific names exist. For example if I have a car that is BWM I would name this picture BWM_1 and so on for more BWM pics, and for a Volvo car I would do the same, I want the code to check this specific part of the car's model, just not sure how to do it.

I did check php manual and many other sources, I couldn't find anything, I did find something about putting it into an array, but what if more pictures are added then I would need to correct the code which I don't want to do...

Thank you very much and sorry for the long post.

  • 写回答

1条回答 默认 最新

  • doumeng3080 2018-02-28 02:55
    关注

    You could try something like this, writing from memory so will probably need debugging...

    //this assumes you always start at 1 and increment by 1
    $countBMW = 1;
    if(file_exists('[~/yourpath/]BMW_'.$countBMW){ //check if any exist
         while(file_exists('[~/yourpath/]BMW_'.$countBMW)){
              $countBMW++; //increase count each time a file exists
         }
    }
    while($countBMW > 0){ //while some images haven't been iterated over...
        echo '<img src="[~/yourpath/]BMW_'.$countBMW.'" [other attributes you need]>'; //print them out
        $countBMW--; //decrease by 1
    }
    

    and repeat for each model, you could store your models in an array, and loop through that, replacing the hard coded 'BMW' text with the array values. You'll need to have a variable variable as well.

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

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用