当前位置: 移动技术网 > IT编程>开发语言>PHP > php生成zip文件类实例

php生成zip文件类实例

2018年06月24日  | 移动技术网IT编程  | 我要评论

本文实例讲述了php生成zip文件类。分享给大家供大家参考。具体如下:

<?php
 /*
  by:   matt ford
  purpose: basic class to create zipfiles
 */
class zipfile {
 public $files = array();
 public $settings = null;
 public $fileinfo = array (
   "name" => "",
   "numfiles" => 0,
   "fullfilepath" => ""
  );
 private $filehash = "";
 private $zip = "";
 public function __construct($settings) {
  $this->zipfile($settings);
 }
 public function zipfile($settings) {
  $this->zip = new ziparchive();
  $this->settings = new stdclass();
  foreach ($settings as $k => $v) {
   $this->settings->$k = $v;
  }
 }
 public function create() {
  $this->filehash = md5(implode(",", $this->files));
  $this->fileinfo["name"] = $this->filehash . ".zip";
  $this->fileinfo["numfiles"] = count($this->files);
  $this->fileinfo["fullfilepath"] = $this->settings->path . 
  "/" . $this->fileinfo["name"];
  if (file_exists($this->fileinfo["fullfilepath"])) {
   return array (
     false,
     "already created: " . $this->fileinfo["fullfilepath"]
     );
  }
  else {
   $this->zip->open($this->fileinfo["fullfilepath"], ziparchive::create);
   $this->addfiles();
   $this->zip->close();
   return array (
     true,
     "new file created: " . $this->fileinfo["fullfilepath"]
     );
  }
 }
 private function addfiles() {
  foreach ($this->files as $k) {
   $this->zip->addfile($k, basename($k));
  }
 }
}
$settings = array (
  "path" => dirname(__file__)
 );
$zipfile = new zipfile($settings);
$zipfile->files = array (
  "./images/navoff.jpg",
  "./images/navon.jpg"
 );
list($success, $error) = $zipfile->create();
if ($success === true) {
 //success
}
else {
 //error because: $error
}
?>

希望本文所述对大家的php程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网