0

I'm using the following to convert CSV to JSON (https://gist.github.com/robflaherty/1185299). I need to need to modify it so that instead of using the exact file url path, it's pulling the newest file url in the directory as it's "source" in $feed.

Any help would be great! I've tried using the code found here PHP: Get the Latest File Addition in a Directory, but can't seem to figure how modify it so that it would work.

<?php
header('Content-type: application/json');

// Set your CSV feed
$feed = 'http://myurl.com/test.csv';

// Arrays we'll use later
$keys = array();
$newArray = array();

// Function to convert CSV into associative array
function csvToArray($file, $delimiter) { 
if (($handle = fopen($file, 'r')) !== FALSE) { 
$i = 0; 
while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) { 
  for ($j = 0; $j < count($lineArray); $j++) { 
    $arr[$i][$j] = $lineArray[$j]; 
  } 
  $i++; 
} 
fclose($handle); 
} 
return $arr; 
} 

// Do it
$data = csvToArray($feed, ',');

// Set number of elements (minus 1 because we shift off the first row)
$count = count($data) - 1;

//Use first row for names  
$labels = array_shift($data);  

foreach ($labels as $label) {
$keys[] = $label;
}

// Add Ids, just in case we want them later
$keys[] = 'id';

for ($i = 0; $i < $count; $i++) {
$data[$i][] = $i;
}

// Bring it all together
for ($j = 0; $j < $count; $j++) {
$d = array_combine($keys, $data[$j]);
$newArray[$j] = $d;
}

// Print it out as JSON
echo json_encode($newArray);

?>
Community
  • 1
  • 1

2 Answers2

0

It's a difficult question to answer because there isn't enough detail. Here are some questions that need answered.

1). Are you creating the csv files that are being read? If you are, you just make sure that the file you want to read is called "latest.csv" and when you go to create "latest.csv" you check for an existing "latest.csv" and rename/archive it first. Your directory then contains archives but the latest one is always of the same name.

2). If you are not creating the csv files then you might want to ask the provider of the csv files if there's a way for you to identify the latest one, as surely, if they are providing them they'd expect to be providing everyone the latest feed and have a mechanism of doing that.

3). If you don't know the provider and want to take a guess, have a look at how the files are named and try to predict the latest one. Eg, if they appear to be including a month and year in them do a file_exists() (if you can) on the predicted next latest file. Again, just a possibility.

Adam
  • 359
  • 3
  • 5
  • Adam, in the long run the CSV file will come from a third party uploaded into a specific directory. Whether that be a remote directory or one that resides on the same server has yet to be seen though I'd prefer remote. In this case, I'd almost rather prefer to pull in any CSV file that has the 'newest' modify/upload date as I'm not sure that any third party will stick to the naming convention but rather to how the file structure is set. – user3768508 Jun 23 '14 at 19:07
0

Based on your comments, if the files reside on the same server or are accessible on a filesystem that supports the file functions, then:

array_multisort(array_map('filemtime', $files=glob('/path/to/*.csv')), SORT_DESC, $files);
$newest = $files[0];

For remote access you could look at something like this: How can I download the most recent file on FTP with PHP?

Community
  • 1
  • 1
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87