I need to open 2 files from my filesystem. Both files are csv-files. I need to determine the differences from both files: So I need to check, how the content has changed from yesterday compared to today.
There will be exactly 1 file every day. The files are labeled according to the timestamp, so there is no real pattern. (to clarify, yea, it is the unix timestamp, but the file isn’t created everyday at the exact same time) For example, the file for yesterday is called: file-1601926309814.csv, the one from the day before (since there is no file created for today yet): file-1601847963649.csv.
I was looking around and found filemtime, but I’m not sure if that helps that much. Another option I came up with, was checking with glob
which files are in the directory, but I’m not sure if that is a good solution.
My code so far:
<?php // Simple script to determine a negative Delta between 2 files // These 2 files are taken from a given destination and pushed // through an array diff. The result will be written in a new // file. $path = "G:\interfaces\out\SAP\Shopartikel\"; $sFileYesterday = "testfile_1.csv"; $sFileToday = "testfile_2.csv"; $aItemsYesterday = array(); $aItemsToday = array(); $aOutput = array(); // get all files from yesterday $fHandleYesterday = fopen($path.$sFileYesterday, "r"); while (($line = fgetcsv($fHandleYesterday, 0, ";")) !== false) { $aItemsYesterday[] = $line[0]; } // get all files from today $fHandleToday = fopen($path.$sFileToday, "r"); while (($line = fgetcsv($fHandleToday, 0, ";")) !== false) { $aItemsToday[] = $line[0]; } // determine the difference $aOutput = array_diff($aItemsYesterday, $aItemsToday); var_dump($aOutput);
As mentioned above, the output is correct, but I need to fetch the correct files as well.
Any ideas?
Advertisement
Answer
You could organize the files in the folder while you are at it anyways.
Use glob as you suggested to get all all filenames and iterate through them and change the names of those with a UNIX timestamp to a date timestamp.
$path = "path"; foreach (glob($path . "*.csv") as $filename) { // only rename files that have not been renamed before. if(strlen($filename) == 17){ // assumes the filename is [UNIX].csv rename($path . $filename, $path . date("Y-m-d", (int)$filename/1000)); } }
This should rename the files to their date and make it easy to pick the correct ones.
An alternative way if you don’t want to rename the files is to create a “translation table” array of the Y-m-d date as key and UNIX name as value.
That way you know $arr[“2020-10-06”] is todays file whatever the UNIX timestamp is.
$path = "path"; foreach (glob($path . "*.csv") as $filename) { $arr[date("Y-m-d", (int)((int)substr($filename, -17)/1000))] = $path . $filename; } $sFileYesterday = $arr[date("Y-m-d", time()-86400)]; $sFileToday = $arr[date("Y-m-d", time())];
This code will become slower and slower for each day since there is one more file each day it needs to “translate”.
You could perhaps speed that up by saving the json of your translation array and find the “new” file using array_diff or something.
Not sure it will be faster than iterating the array and doing the “translation” but you will have to see when it becomes slow.
Example: https://3v4l.org/cb4Mp