doy57007 2013-05-25 02:50
浏览 225
已采纳

如何读取服务器上的所有现有CSV文件?

This question is a little complicated, but I will try my best to explain. Feel free to ask for clarification if my question does not make sense.

Situation:

On my server there will be a new CSV file added every day into a particular folder. The CSV files will all be kept in the same folder. They will have a name consistant with the day. For example log_2013_05_24.csv.

Objective:

I need to write a PHP program that will find all existing CSV files so that I can search through them. In the end I need to find all the dates that exist.

The data inside the CSV files looks similar to this:

4/14/13, 17:30:56, data, data, data, data
4/14/13, 17:31:06, data, data, data, data 
( ... about 10,000 of these ^ a new line for every 10 seconds all day )

I am using php explode to get the dates from my current CSV file. This will produce an array of strings similar to: '4/14/13'.

But there are two problems:

1: They are strings, not dates

2: I don't know how to find all existing CSV files so that PHP can search them.

  • 写回答

3条回答 默认 最新

  • duanche8554 2013-05-25 03:00
    关注

    Use DateTime class to create dates

    $str = '04/14/13';
    $date = new DateTime($str);
    echo $date->format('Y-m-d');
    

    Use glob() function to traverse filesystem

    $files = glob('*.csv');
    var_dump(files);
    

    Sample output:

    array(3) {
      [0]=>
      string(8) "test.csv"
      [1]=>
      string(9) "test1.csv"
      [2]=>
      string(9) "test2.csv"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看