Reputation: 47
I would like to use this script to download the XML feed which is updated every 20 min. The problem is the file name has a date and time stamp. Can someone point me in the right direction how to change it so it will always picks the newest file so i can update the stock.
Sample file names:
Feed2842020175940.xml
Feed2842020173907.xml
Feed2842020171807.xml
Calling the function from the 'Download from URL' option on Step 1:
[custom_file_download("ftp://username:[email protected]/path/to/file.xml","xml")]
Function itself:
// Programmatically download and return import file via URL.
function custom_file_download($url, $type = 'xml'){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
/* Optional: Set headers...
* $headers = array();
* $headers[] = "Accept-Language: de";
* curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
*/
$result = curl_exec($ch);
if (curl_errno($ch)) {
exit('Error:' . curl_error($ch));
}
curl_close ($ch);
$uploads = wp_upload_dir();
$filename = $uploads['basedir'] . '/' . strtok(basename($url), "?") . '.' . $type;
if (file_exists($filename)){
@unlink($filename);
}
file_put_contents($filename, $result);
return str_replace($uploads['basedir'], $uploads['baseurl'], $filename);
}
Upvotes: 1
Views: 580
Reputation: 5693
I see two ways to do this:
Have a logic on the server with the XML files where the newest file will always have the same name (e.g. latest.xml
). You could probably achieve this by using symbolic links.
Work back from the current timestamp (i.e. using time()
or another way to get a timestamp in the format the filenames are using) to deduce the latest filename on the server. I don't see how else this can be done except to decrement the timestamp by 1 then check if a file with that timestamp in the name exists, then decrement by 1 again and check, etc. This option is not a good idea. :)
Upvotes: 0