用于提供文件最新版本的PHP脚本

时间:2013-03-14 13:09:28

标签: php

我的PHP生锈了,我希望有人可以用快速的脚本帮助我 - 我真的不知道从哪里开始!

我有一个包含各种版本软件产品的压缩档案的文件夹:

  • Product_1.00.zip
  • Product_1.05.zip
  • Product_2.00.zip

现在,在我的网站上,我有一个下载产品的按钮。但是,我希望该按钮始终下载最新版本。

在我看来,一个很好的解决方案是链接到一个PHP脚本,该脚本将扫描文件夹中的最新版本并将该文件传递给用户,就像他们已将浏览器直接指向该文件一样。

有人能为我提供一个起点吗?

4 个答案:

答案 0 :(得分:2)

我认为将目录中的文件读入数组是最容易的。然后natsort数组并弹出最后一个条目。

以下是一个例子:

<?php
function getLatestVersion() {
    $dir = dir('.');
    $files = array();

    while (($file = $dir->read()) !== false) {
        $files[] = $file;
    }
    $dir->close();

    natsort($files);
    return array_pop($files);
}

输出

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"
}

如何下载最新版本的zip文件?

修改

就像@j_mcnally在下面的评论中指出的那样,让网络服务器处理静态文件的提供效率更高。可能的方法是直接链接或使用301将请求从PHP文件重定向到正确的位置。

但是如果你还想让PHP做的话。这是一个例子。


http://perishablepress.com/http-headers-file-downloads抓取下面的示例并对其进行了一些修改。

<?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);
?>

答案 1 :(得分:1)

这应该有效。当然,可以添加更多检查,具体取决于该文件夹中可以存储的内容;如果文件夹内容包含太多文件等,您也可能会改变阅读文件夹内容的方式。 对于字符串比较,此代码中的关键字可能为strnatcmp()

<?php
$files = scandir('/path/to/files');
$result = array_reduce(
    $files,
    function($a, $b) {
        $tpl = '/^Product_(.+).zip$/';
        // return second file name if the first file doesn't follow pattern Product_XXX.zip
        if (!preg_match($tpl, $a)) {
            return $b;
        }
        // return first file name if the second file doesn't follow pattern Product_XXX.zip
        if (!preg_match($tpl, $b)) {
            return $a;
        }
        return strnatcmp($a, $b) >= 0 ? $a : $b;
    },
    ''
);

答案 2 :(得分:0)

以下内容将在downloads目录中查找,找到最新文件(通过查看文件的修改时间)并返回最新文件的名称:

$dir = dir("downloads");
$files = array();
while (($file = $dir->read()) !== false) {
    $files[filemtime($file)] = $file;
}
$dir->close();

ksort($files);
$fileToDownload = $files[0];

希望这有帮助!

答案 3 :(得分:0)

此代码通过使用文件修改时间来确定给定目录中的最新版本,可能使用文件名上的正则表达式将是一种更好的方法,但这证明了PHP的DirectoryIterator。     

$files = array();

foreach(new DirectoryIterator("productZips/") as $fileInfo) {

  if(!$fileInfo->isFile()) continue;
  $files[$fileInfo->getMTime()] = $fileInfo->getFilename();
}

ksort($files);
$latestFile = array_pop($files);

您可以在此处阅读更多内容:http://php.net/manual/en/class.directoryiterator.php