dongshi4589 2013-07-24 11:42
浏览 32
已采纳

PHP - 搜索并替换然后排序和回显

I've managed (well, stackoverflow has shown me how) to search a directory on my server and echo and image. The trouble is the images in the folder are named by an IP camera yy_mm_dd_hh_mm where dd (and other date digits) have either one or two digits. I need them to have a preceeding zero so that I don't end up with, for example, an image taken at 9:50am being treated as a higher value than the photo taken more recently, at 10:05am. (I want it to treat it as 09_50 and 10_05 to fix the issue).

I've looked at search and replace but cannot get this to work within my current code:

function webcam_image () {
foreach (glob( "../camera/IPC_IPCamera*.jpg") as $f ) {
$list[] = $f;
}
sort($list);
echo array_pop($list);   
}

example file = IPC_IPCamera_13_7_24_9_57_45.jpg

any help would be much appreciated!

Thanks

Ali

  • 写回答

4条回答 默认 最新

  • down100009 2013-07-24 11:56
    关注

    I would ignore the file name altogether and use a DirectoryIterator, fetching the files actual modified date. Something like this might work.

    $files = array();
    $iterator = new \DirectoryIterator('/path/to/dir');
    foreach ($iterator as $file) {
      if(! $file->isDot()) $files[$file->getMTime()] = $file->getFilename();
    }
    ksort($files);
    
    print_r($files);
    

    Take a look here for more information: http://php.net/manual/en/class.directoryiterator.php

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

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?