I think it's the easiest to read the files from the directory into an array. Then natsort
the array and pop off the last entry.
Here is an example:
<?php
function getLatestVersion() {
$dir = dir('.');
$files = array();
while (($file = $dir->read()) !== false) {
$files[] = $file;
}
$dir->close();
natsort($files);
return array_pop($files);
}
Outputs
array(6) {
[0]=>
string(1) "."
[1]=>
string(2) ".."
[2]=>
string(16) "Product_1.00.zip"
[3]=>
string(16) "Product_1.05.zip"
[5]=>
string(16) "Product_2.00.zip"
[4]=>
string(17) "Product_10.00.zip"
}
How to download the latest version of the zip file?
EDIT
Like @j_mcnally pointed out in his comment below it's more efficient to let the webserver handle the serving of static files. Possible ways are direct links or redirecting the request from the PHP file to the right location using a 301
.
But if you still want to let PHP do the work. Here is an example.
Grabbed the example below from http://perishablepress.com/http-headers-file-downloads and altered it a bit.
<?php // HTTP Headers for ZIP File Downloads
// http://perishablepress.com/press/2010/11/17/http-headers-file-downloads/
// set example variables
// Only this line is altered
$filename = getLatestVersion();
$filepath = "/var/www/domain/httpdocs/download/path/";
// http headers for zip downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
ob_end_flush();
@readfile($filepath.$filename);
?>