PHPÉú³ÉѹËõÎļþʵÀý

5年以前  |  阅读数:926 次  |  编程语言:PHP 

´o¸ÅÐeÇo£º

ÿһ¸o¶(C)µ¥¶¼Óжa¸oÎļþ¸½¼þ£¬ÔÚÏÂÔصÄʱºoÏ£Íu¶Ôµ±Ç°¶(C)µ¥µÄÎļþ×Ô¶¯´o°u³ÉÒ»¸oѹËo°uÏÂÔØ

ϸ½ÚÐeÇo£ºµ±Ç°¶(C)µ¥ºÅ_ÄeÔÂÈÕ+ʱ¼a.zip ÀýÈ磺

1.Éu³ÉѹËoÎļþ£¬Ñ¹ËoÎļþÃu¸ñʽ£º

2.ѹËoÎļþ´ae*ÅÔÚ¸uĿ¼ /upload/zipfile/ÄeÔÂ/×Ô¶¨ÒaµÄѹËoÎļþÃu.zip

3.µa»÷ÏÂÔØѹËo°u£¬ÏµÍ³¿ªÊ¼¶ÔѹËoÎļþ´o°u£¬´o°uÍe³Éºo×Ô¶¯¿ªÊ¼ÏÂÔØ

4.ΪÁËÀÖ¹±(C)¶ѹËo°uÎļþ¾¶£¬ÐeÒª¶ÔÏÂÔصÄѹËo°uÎļþÃu¸ÄÃu

¾ßÌa²Ù×÷ģʽÇe¼uÏÂÃaeµÄ´uÂe£º

ÎļþÂ*¾¶£º

ѹËo°uÎļþ´aež¶£º/upload/zipfile/

ÉÏ´«µÄ¸½¼þ´aež¶£º/upload/file/

1.»u±¾ÅaÖÃÎļþÎļþ config.inc.php*ÅÔÚϵͳ¸uĿ¼


    define('SYS_ROOT', str_replace("\\", '/', dirname(__FILE__)));
    define('SYS_UPLOAD', SYS_ROOT.'/upload/file');
    define('SYS_DOWNLOAD', SYS_ROOT.'/upload/zipfile');
    define('SYS_WIN', strpos(strtoupper(PHP_OS), 'WIN') !== false ? true: false);
    define('SYS_CHMOD', ('0777' && !SYS_WIN) ? '0777' : 0);

2.ѹËo°u³ÌÐo´uÂeÎļþ getzip.php


    header("Content-type: text/html; charset=utf-8");
    require_once '../config.inc.php'; //ÔØÈeÅaÖÃÂ*¾¶ÅaÖÃÎļþ
    $arrfiles = array(SYS_UPLOAD . '/1.jpg',
      SYS_UPLOAD . '/x.jpg',); //ÕaÀiÊǸ½¼þµÄÎļþÊý×e
    $orderNum = '888'; //¶(C)µ¥ºÅ
    $downFileName = 'tieniu.zip'; //ÏÂÔصÄÎļþÃu Èç¹uΪ¿ÕÄÇô¾ÍÊÇϵͳ×Ô¶¨ÒaÃu³Æ Èç¹uÖ¸¶¨¾ÍÏÔʾָ¶¨Ãu×Ö
    $zipUrl = create_zip($arrfiles, $orderNum); //Éu³ÉµÄѹËoÎļþÃu´Ê
    file_down($zipUrl, $downFileName); //Ìa¹(C)httpÏÂÔØ£¬²¢¿ÉÒÔ½øÐÐÖØÃuÃuÏÂÔØÎļþ,½¨ÒeÖØÃuÃu£¬*ÀÖ¹Â*¾¶²Â½a

    /*
     * Éu³ÉѹËo°uÎļþÃu
     * @param [String] $orderNum ¶(C)µ¥ºÅ
     * @return [String] *µ»Ø´øÓоø¶ÔÂ*¾¶µÄ¶(C)µ¥ºÅµÄѹËoÎļþÃu
     */

    function get_zipname($orderNum) {
      $zipName = SYS_DOWNLOAD . '/' . date('Ym') . '/' . $orderNum . '_' . date("Ymd_Hi") . '.zip';
      return $zipName;
    }

    /*
     * °´ÕÕÌض¨ÐeÇo´o°uѹËo°uµÄĿ¼½a¹¹ÉeÖÃ
     */

    function pack_object() {

    }

    /*
     * Éu³ÉѹËo°u
     * @param [Array] $arrfiles ´øÓоø¶ÔÂ*¾¶µÄÎļþÊý×e
     * @param [String] $orderNum ¶(C)µ¥ºÅ
     * @return [String] *µ»Ø´øÓоø¶ÔÂ*¾¶µÄ¶(C)µ¥ºÅµÄѹËoÎļþÃu ÈçÈç¹uʧ°Ü*µ»Ø FALSE
     */

    function create_zip($arrfiles, $orderNum) {
      $zipName = get_zipname($orderNum); //»ñµÃÎļþÃu
      dir_create(dirname($zipName)); //½¨Á¢Éu³ÉѹËoÎļþµÄĿ¼
      $zip = new ZipArchive();
      if ($zip->open($zipName, ZIPARCHIVE::CREATE) !== TRUE) {
        return FALSE;
      }
      foreach ($arrfiles as $path) {
        if (is_file($path)) {//ÅжÏÎļþÊÇ*ñ´aeÔÚ
          $zip->addFile($path, basename($path)); //°ÑÎļþ¼ÓÈeµ½Ñ¹Ëo°uÖÐ
        }
      }
      $zip->close();
      return $zipName;
    }

    /*
     * ´¦ÀiÎļþĿ¼
     * @param [Array] $arrfiles ´øÓоø¶ÔÂ*¾¶µÄÎļþÊý×e
     * @param [String] $dirpath ÎļþÂ*¾¶
     * @return [String] *µ»Ø´¦ÀiµÄÎļþÂ*¾¶£¬*½±aÉu³ÉÎļþĿ¼
     */

    function dir_path($dirpath) {
      $dirpath = str_replace('\\', '/', $dirpath);
      if (substr($dirpath, -1) != '/')
        $dirpath = $dirpath . '/';
      return $dirpath;
    }

    /*
     * Éu³ÉÎļþĿ¼
     * @param [String] $path ÎļþÂ*¾¶
     * @return [String] *µ»ØÉu³ÉµÄÎļþĿ¼½a¹¹
     */

    function dir_create($path) {
      if (is_dir($path))
        return true;
      $dir = str_replace(SYS_DOWNLOAD . '/', '', $path);
      $dir = dir_path($dir);
      $temp = explode('/', $dir);
      $cur_dir = SYS_DOWNLOAD . '/';
      $max = count($temp) - 1;
      for ($i = 0; $i < $max; $i++) {
        $cur_dir .= $temp[$i] . '/';
        if (is_dir($cur_dir))
          continue;
        @mkdir($cur_dir);
        if (SYS_CHMOD)
          @chmod($cur_dir, SYS_CHMOD);
        if (!is_file($cur_dir . '/index.html') && !is_file($cur_dir . '/index.php'))
          file_copy(SYS_ROOT . '/upload/index.html', $cur_dir . '/index.html');
      }

      return is_dir($path);
    }

    /*
     * ÎļþCOPY
     * @param [String] $from copyÔ´Îļþ
     * @param [String] $to copyÎļþÄ¿µÄµØ
     * @return [Bool] ³É¹¦ ture ʧ°Ü false
     */

    function file_copy($from, $to) {
      dir_create(dirname($to));
      if (is_file($to) && SYS_CHMOD)
        @chmod($to, SYS_CHMOD);
      if (@copy($from, $to)) {
        if (SYS_CHMOD)
          @chmod($to, SYS_CHMOD);
        return true;
      } else {
        return false;
      }
    }

    /*
     * ÎļþÏÂÔØ´¦Àiº¯Êý
     * @param [String] $file ÎļþÂ*¾¶
     * @param [String] $filename ÏÂÔØʱ¼aÖØÐÂÃuÃuµÄÎļþÃu
     * @param [String] $data ÏÂÔØÎļþÌi×°µÄÊý¾ÝÄÚÈÝ
     */

    function file_down($file, $filename = '', $data = '') {
      if (!$data && !is_file($file))
        exit;
      $filename = $filename ? $filename : basename($file);
      $filetype = file_ext($filename);
      $filesize = $data ? strlen($data) : filesize($file);
      ob_end_clean();
      @set_time_limit(0);
      if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) {
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
      } else {
        header('Pragma: no-cache');
      }
      header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
      header('Content-Encoding: none');
      header('Content-Length: ' . $filesize);
      header('Content-Disposition: attachment; filename=' . $filename);
      header('Content-Type: ' . $filetype);
      if ($data) {
        echo $data;
      } else {
        readfile($file);
      }
      exit;
    }

    function file_ext($filename) {
      return strtolower(trim(substr(strrchr($filename, '.'), 1)));
    }

    //´Ëº¯ÊýδÓõ½£¬ÓÃÀ´×oÕu¸oĿ¼µÄ´o°uÏÂÔØ
    function listdir($start_dir = '.') {
      $files = array();
      if (is_dir($start_dir)) {
        $fh = opendir($start_dir);
        while (($file = readdir($fh)) !== false) {
          if (strcmp($file, '.') == 0 || strcmp($file, '..') == 0)
            continue;
          $filepath = $start_dir . '/' . $file;
          if (is_dir($filepath))
            $files = array_merge($files, listdir($filepath));
          else
            array_push($files, $filepath);
        }
        closedir($fh);
      } else {
        $files = false;
      }
      return $files;
    }

3.PHP³ÌÐoÉu³ÉѹËoÎļþÐeÒªÓõ½Ñ¹ËoÀa£ºZipArchive

Õa¸oÊÇphpµÄÀ(C)Õ¹Àa£¬×Ôphp5.2°ae±¾ÒÔºo¾ÍÒѾ­Ö§³ÖÕa¸oÀ(C)Õ¹£¬Èç¹uÄaÔÚʹÓõÄʱºo³oÏÖ´iÎo£¬²e¿´ÏÂphp.iniÀiÃaeµÄextension=php_zip.dllÇ°ÃaeµÄ*ÖºÅÓÐûÓÐÈ¥µo£¬È»ºoÔÙÖØÆoApacheÕaÑu²ÅÄÜʹÓÃÕa¸oÀa¿a¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例