php生成zip文件類實例_PHP教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:php生成圖片縮略圖的方法
      具體如下: 這里需要用到GD2 library

      這篇文章主要介紹了php生成zip文件類,實例分析了php操作zip文件的技巧,非常具有實用價值,需要的朋友可以參考下

      本文實例講述了php生成zip文件類。分享給大家供大家參考。具體如下:

      1. <?php 
      2.  /* 
      3.   By:   Matt Ford 
      4.   Purpose: Basic class to create zipfiles 
      5.  */ 
      6. class zipFile { 
      7.  public $files = array(); 
      8.  public $settings = NULL; 
      9.  public $fileInfo = array ( 
      10.    "name" => ""
      11.    "numFiles" => 0, 
      12.    "fullFilePath" => "" 
      13.   ); 
      14.  private $fileHash = ""
      15.  private $zip = ""
      16.  public function __construct($settings) { 
      17.   $this->zipFile($settings); 
      18.  } 
      19.  public function zipFile($settings) { 
      20.   $this->zip = new ZipArchive(); 
      21.   $this->settings = new stdClass(); 
      22.   foreach ($settings as $k => $v) { 
      23.    $this->settings->$k = $v
      24.   } 
      25.  } 
      26.  public function create() { 
      27.   $this->fileHash = md5(implode(","$this->files)); 
      28.   $this->fileInfo["name"] = $this->fileHash . ".zip"
      29.   $this->fileInfo["numFiles"] = count($this->files); 
      30.   $this->fileInfo["fullFilePath"] = $this->settings->path .  
      31.   "/" . $this->fileInfo["name"]; 
      32.   if (file_exists($this->fileInfo["fullFilePath"])) { 
      33.    return array ( 
      34.      false, 
      35.      "already created: " . $this->fileInfo["fullFilePath"
      36.      ); 
      37.   } 
      38.   else { 
      39.    $this->zip->open($this->fileInfo["fullFilePath"], ZIPARCHIVE::CREATE); 
      40.    $this->addFiles(); 
      41.    $this->zip->close(); 
      42.    return array ( 
      43.      true, 
      44.      "new file created: " . $this->fileInfo["fullFilePath"
      45.      ); 
      46.   } 
      47.  } 
      48.  private function addFiles() { 
      49.   foreach ($this->files as $k) { 
      50.    $this->zip->addFile($kbasename($k)); 
      51.   } 
      52.  } 
      53. $settings = array ( 
      54.   "path" => dirname(__FILE__
      55.  ); 
      56. $zipFile = new zipFile($settings); 
      57. $zipFile->files = array ( 
      58.   "./images/navoff.jpg"
      59.   "./images/navon.jpg" 
      60.  ); 
      61. list($success$error) = $zipFile->create(); 
      62. if ($success === true) { 
      63.  //success 
      64. else { 
      65.  //error because: $error 
      66. ?> 

      分享:php獲取網頁里所有圖片并存入數組的方法
      本文實例講述了php獲取網頁里所有圖片并存入數組的方法。分享給大家供大家參考。具體如下: 希望本文所述對大家的php程序設計有所幫助。

      來源:模板無憂//所屬分類:PHP教程/更新時間:2015-04-08
      相關PHP教程