【问题标题】:How to open the newest csv file of a directory in array using php? [duplicate]如何使用php打开数组中目录的最新csv文件? [复制]
【发布时间】:2019-03-24 07:50:33
【问题描述】:

所以我有这个设备,它创建一个 CSV 文件并每 15 分钟更新一次文件中的数据,然后每天向我们的服务器发送一个新的 csv 文件。所以我想在一个网站上显示最新的数据。

我有一行代码可以使用 fgetcsv 加载文件,但这段代码会打开特定文件夹中的特定 csv 文件。

$f_pointer=fopen("database/student01.csv","r"); // file pointer
$ar=fgetcsv($f_pointer);
print_r($ar);

我想找到一种方法来打开最新的 csv 文件和数组中的数据,以便我可以根据特定数据制作表格。

我是 Web 开发的新手,如果有任何有用的资源,我将不胜感激。谢谢。


更新

我找到了打开目录中最新文件的方法

function pre_r($array) {
  echo '<pre>';
  print_r($array);
  echo '</pre>';
}

$files = scandir('database/AWLR/2018/10', SCANDIR_SORT_DESCENDING);
$newest_file = $files[0];

pre_r($newest_file); //file name

//to open file in array
$f_pointer=fopen("database/AWLR/2018/10/$newest_file","r"); // file pointer
$ar=fgetcsv($f_pointer);
pre_r($ar);

但是这段代码打开了 csv 文件的第一行,而我想打开最后一行(因为它是给出的最新数据)。有没有办法自动更改目录?因为这段代码每个月都要更新


最新更新

此代码有效,不幸的是它显示了 csv 文件的第一行,

$current_year = date('Y'); 
$current_month = date('m');
$files = scandir("database/AWLR/".$current_year."/".$current_month, 1); // use 0 for ascending order, and 1 for descending order
$newest_file = $files[0];

pre_r($newest_file); //file name

//to open file in array
$f_pointer=fopen("database/AWLR/".$current_year."/".$current_month."/".$newest_file,'r'); // file pointer
$ar=fgetcsv($f_pointer);
pre_r($ar);

有没有办法显示csv文件的最后一行?

这是我正在谈论的 csv 文件之一

csv file

我已经解决了,谢谢大家

【问题讨论】:

  • 这是什么设备?此设备可以在文件上设置名称吗?使用 Unix 说文件名?表示编号最大的文件名是最新的文件。
  • 它是一个数据记录器,一个遥测设备,它可以设置一个日期格式的名称(例如:20181004.CSV),并且每天都会发送一个新的csv文件。
  • 这称为不变性和协变。如果你用谷歌搜索,你会发现教程比我们在此处的答案中解释得更好。

标签: php arrays database csv


【解决方案1】:

这是您可以使用和测试的建议代码

$current_year = date('Y'); 
$current_month = date('m');
$files = scandir("database/AWLR/".$current_year."/".$current_month, 1); // use 0 for ascending order, and 1 for descending order
$newest_file = $files[0];

pre_r($newest_file); //file name

//to open file in array
$f_pointer=fopen("database/AWLR/".$current_year."/".$current_month."/".$newest_file,'r'); // file pointer
$ar=fgetcsv($f_pointer);
pre_r($ar);

【讨论】:

  • 感谢您的帮助,我会尝试代码并告知结果。
  • @Waloy,谢谢。如果它有效,请投票给这个答案。
  • 它可以工作,但代码显示 csv 文件的第一行,而我想显示最后一行(最新数据)。我在我的帖子中包含了一个 csv 文件。知道如何显示最后一行 php 或任何其他语言吗?
【解决方案2】:
    //this is your file path
    $files_location = "database/";

    //this function will return latest file in folder 
    function latest_file_Name ($files_location) {
    //open folder and reads whats inside
    $openDir = opendir($files_location);
    //look for each file in the folder. 
    while (false !== ($fileName = readdir($openDir))) {
    //ignore the  . and .. these are system folders. you can also ignore any other file if not necesery
    // for Example if($fileName != "." && $fileName != ".." && $fileName != "unwanted_file_name.csv)" in condition 
    if($fileName != "." && $fileName != "..")
    {
    $list[date("YmdHis ", filemtime($files_location.$fileName))]=$fileName; 
    }
    }
    rsort($list);
    $last_file =array_shift(array_values($list));
    return $last_file;
    }

    $last_file_in_folder = latest_file_Name($files_location);
    $f_pointer=fopen("database/".$last_file_in_folder,"r"); // file pointer
    $ar=fgetcsv($f_pointer);
    print_r($ar);

试试这个,希望能解决你的问题。

【讨论】:

  • 感谢您的帮助,我会尝试代码并告知结果。谢谢你。这是一个很棒的社区!
  • 祝你好运@Waloy 无论文件名或类型如何,这个答案都会起作用.....
猜你喜欢
  • 1970-01-01
  • 2018-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-30
  • 1970-01-01
相关资源
最近更新 更多