Skip to content

Instantly share code, notes, and snippets.

@TemaSM
Last active February 13, 2017 05:08
Show Gist options
  • Save TemaSM/b2670cdc21f2aa338523e053cf6ea72b to your computer and use it in GitHub Desktop.
Save TemaSM/b2670cdc21f2aa338523e053cf6ea72b to your computer and use it in GitHub Desktop.
[PHP] Get file by latest version from current directory (electron-builder / nsis-web)
<?
// Author: Tema Smirnov [https://github.com/TemaSM] © 2017
$iVersion= 0;
$filename = "<filename>.ext";
$regexp = "/([-A-z]+)-([\.0-9]+)-([a-z]+)-([a-z0-9]+).+nsis.7z/";
/* Example: MyProject-0.0.1-alpha-x64.nsis.7z
0 => MyProject-0.0.1-alpha-x64.nsis.7z
1 => MyProject
2 => 0.0.1
3 => alpha
4 => x64
*/
foreach (new DirectoryIterator(__DIR__) as $file) {
if ($file->isFile()) {
preg_match($regexp, $file->getFilename(), $matches);
$ver = str_replace('.', '', $matches[2]);
if ($ver > $iVersion) {
$iVersion= $ver;
$filename = $matches[0];
} // print $file->getFilename() . "\n";
}
}
$URL = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$fileURL = $URL . $filename;
// echo $fileURL;
header('Location: '.$fileURL);
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment