dragoninasia2014 2013-03-14 13:09
浏览 23
已采纳

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

My PHP's rusty and I'm hoping that someone can help me with a quick script - I don't really know where to start with it!

I have a folder containing zipped archives of various versions of a software product:

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

etc.

Now, on my web site I have a button to download the product. However, I'd like that button to always download the latest version.

It seems to me that a nice solution would be to link to a PHP script that would scan the folder for the latest version and deliver that file to the user as if they have pointed the browser directly at the file.

Can anyone provide me with a starting point?

  • 写回答

4条回答 默认 最新

  • duanshai4484 2013-03-14 13:17
    关注

    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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程