title: PHP Get Newest File Path
body: <p>I'm using the following to convert CSV to JSON (<a href="https://gist.github.com/robflaherty/1185299" rel="nofollow noreferrer">https://gist.github.com/robflaherty/1185299</a>). 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.</p>
  
  <p>Any help would be great! I've tried using the code found here <a href="https://stackoverflow.com/questions/1491020/php-get-the-latest-file-addition-in-a-directory">PHP: Get the Latest File Addition in a Directory</a>, but can't seem to figure how modify it so that it would work.</p>
  
  <pre><code>&lt;?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 &lt; 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 &lt; $count; $i++) {
  $data[$i][] = $i;
  }
  
  // Bring it all together
  for ($j = 0; $j &lt; $count; $j++) {
  $d = array_combine($keys, $data[$j]);
  $newArray[$j] = $d;
  }
  
  // Print it out as JSON
  echo json_encode($newArray);
  
  ?&gt;
  </code></pre>