发布新日志

  • My first PHP script

    2008-08-06 16:03:57

    今天有个机会要写一段php脚本,需求如下:
    Provide a URL link for HTTP request for the latest File (actual file name) generated by the application and stored on our server.
        a. this link looks like below,
           http://[ip address]:[port]/filename.bin?     
        b. can accept user's HTTP request
        c. can return the latest files to user
            -- read the file number from an existing file [say fileCur.dat];This file contains the latest file number
               generated by the application
            -- get the latest file which is suffixed with the latest file number

    刚拿到需求有点蒙,不知道该用什么语言写,偶是编程菜鸟。去网上查了一圈,定了用php,不过.bin的link还不知道如何用php来实现,所以暂改了需求定为:
    http://[ip address]:[port]/filename.php?

    代码先贴下,偶是一步步的网上查的再结合实际情况加工的,也许看了会觉得有点熟悉,呵呵。

    <?php

    //
    $_SERVER['scrīpt_FILENAME'] returns the absolute path of the scrīpt stored on the sever, however it contains some strings that I don't need, so do a search and replace here to meet my requiremnt.
    $file_dir = str_replace("filename.php","bin files/",$_SERVER['scrīpt_FILENAME']);
    $file_CurFileNum =$file_dir.'CurFile.dat';

    // Get the latest file number
    $CurFileNum=GetLatestFileNumber($file_CurFileNum);

    // Download latest AGPSData file
    Filedw('AGPSData');

    //Function: File Download
    Function Filedw($FileType)
    {
     global $CurFileNum,$file_dir;
     $file_name=$FileType.'_'.$CurFileNum.'.bin';
      header("Content-type: application/octet-stream");
      header('Content-length: '.filesize($file));
    // display as filename.bin instead of the real file name when downloding on the client
      header('Content-disposition: attachment; filename=filename.bin');
      readfile($file_dir.$file_name,50000);
    }

    //Function: Check if file exists
    function CheckFileExists($File)
    {
      if (is_readable($File) == false)
      { die($File.' file does not exist.');}
      else
      { echo $File.'file exists';}
    }

    // Function: Retrieve the latest file number.
    function GetLatestFileNumber($file)
    {
      $fso = fopen($file, 'r');
      $LatestFileNumber = fread($fso, filesize($file));
      fclose($fso);
      $LatestFileNumber=trim($LatestFileNumber);
      return $LatestFileNumber;
    }

    ?>

我的栏目

我的存档

数据统计

  • 访问量: 730
  • 日志数: 1
  • 建立时间: 2008-08-06
  • 更新时间: 2008-08-06

RSS订阅

Open Toolbar