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.

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

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)