当前位置:首页 > PHP教程 > php文件操作 > 列表

php在线文件与文件夹压缩实例代码

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-09 16:08:24 浏览: 评论:0 

注明这款php文件压缩代码他要借助于zip.exe文件,所以我们要把zip.exe压缩文件给拿出来,实例代码如下:

  1. <?php 
  2.  
  3. if ( !IsSet($_GET['dirname']) ) 
  4. { 
  5.  show_input_form() ; 
  6. } 
  7. else 
  8. { 
  9.  // check if empty 
  10.  if ( emptyempty($_GET['dirname']) ) 
  11.  { 
  12.   hg_exit("请输入文件夹名!") ; 
  13.  } 
  14.  
  15.  // check valid dirname 
  16.  if ( FALSE !== strpos($_GET['dirname'], "/") ) 
  17.  { 
  18.   hg_exit(""/" 是非法的文件夹名!") ; 
  19.  } 
  20.  if ( FALSE !== strstr($_GET['dirname'], "..") ) 
  21.  { 
  22.   hg_exit("".." 是非法的文件夹名!") ; 
  23.  } 
  24.  
  25.  // check valid dir 
  26.  if ( !is_dir($_GET['dirname']) ) 
  27.  { 
  28.   hg_exit(""{$_GET['dirname']}" 不是一个有效的文件夹!") ; 
  29.  } 
  30.  
  31.  $szData = "" ; 
  32.  $szInfo = "" ; 
  33.  
  34.  $file_count = @ZipDir($_GET['dirname'], &$szData, &$szInfo) ; 
  35.  $info_size_16byte = @sprintf("%016d", @strlen($szInfo)) ; 
  36.  $szData = @sprintf("%016d",$file_count) . $info_size_16byte . $szInfo . $szData ; 
  37.  $filename = $_GET['dirname'] . ".dat" ; 
  38.  if ( function_exists(gzencode) ) 
  39.  { 
  40.   $szData = gzencode($szData) ; 
  41.   $filename .= ".gz" ; 
  42.  } 
  43.  
  44.  Header("Content-type: application/octet-stream"); 
  45.  Header("Accept-Ranges: bytes"); 
  46.  Header("Accept-Length: " . strlen($szData)); 
  47.  Header("Content-Disposition: attachment; filename=$filename"); 
  48.  
  49.  echo $szData ; 
  50. } 
  51.  
  52.  
  53. function show_input_form() 
  54. { 
  55.  echo HtmlHead("文件打包") ; 
  56.  echo "<form name="input">n" 
  57.   . "请输入要打包的文件夹,注意,仅当前目录下的文件夹才可以下载!<p>n" 
  58.   . "<input name="dirname">n" 
  59.   . "<input type="button" value="确定" onClick="show_download_link(dirname.value);">n" 
  60.   . "</form>n" ; 
  61.  echo "<script>n" 
  62.   . "input.dirname.focus();n" 
  63.   . "function show_download_link(dir)n" 
  64.   . "{" 
  65.   . "   var top = (screen.height-200)/2 ;n" 
  66.   . "   var left = (screen.width-300)/2 ;n" 
  67.   . "   newwin=window.open('', '', 'width=300,height=200,top=' + top + ',left=' + left + ', resizable=0,scrollbars=auto');n" 
  68.   . "   url = "{$_SERVER['PHP_SELF']}" + "?dirname=" + dir ;n" 
  69.   . "   newwin.document.write('<a href=' + url + '>点击此链接下载,<br>或者右键点击此处选择"另存为"</a>');n" 
  70.   . "}" 
  71.   . "</script>n" ; 
  72.  echo HtmlFoot() ; 
  73. } 
  74.  
  75.  
  76. function ZipDir($szDirName, &$szData, &$szInfo) 
  77. { 
  78.  // write dir header 
  79.  $szInfo .= "$szDirName|[dir]n" ; 
  80.  $file_count = 0 ; 
  81.  $hDir = OpenDir($szDirName) ; 
  82.  while ( $file = ReadDir($hDir) ) 
  83.  { 
  84.   if ( $file=="." || $file==".." ) continue ; 
  85.  
  86.   $szCurFile = "$szDirName/$file" ; 
  87.  
  88.   if ( Is_Dir($szCurFile) ) 
  89.   { 
  90.    $file_count += ZipDir($szCurFile, &$szData, &$szInfo) ; 
  91.   } 
  92.   else if ( Is_File($szCurFile) ) 
  93.   { 
  94.    $hCurFile = fopen($szCurFile, "rb") ; 
  95.    $size = filesize($szCurFile) ; 
  96.    $szStream = fread( $hCurFile, $size ) ; 
  97.    fclose($hCurFile) ; 
  98.    $file_count++ ; 
  99.  
  100.    // write info 
  101.    $szInfo .= "$szCurFile|$sizen" ; 
  102.  
  103.    // write data 
  104.    $szData .= $szStream ; 
  105.   } 
  106.  } 
  107.  
  108.  // write dir footer 
  109.  $szInfo .= "$szDirName|[/dir]n" ; 
  110.  return $file_count ; 
  111. } 
  112.  
  113.  
  114. function hg_exit($str) 
  115. { 
  116.  echo HtmlHead("Error, exit!") ; 
  117.  echo "<h5>" . $str . "</h5>" ; 
  118.  echo HtmlFoot() ; 
  119.  exit ; 
  120. } 
  121.  
  122.  
  123. function HtmlHead($title) 
  124. { 
  125.  return "<html>nn<head>n" 
  126.   . "<meta http-equiv="Content-Type" content="text/html; charset=gb2312">n" 
  127.   . "<style type="text/css教程">n" 
  128.   . "body,input,td{font:12px verdana}n" 
  129.   . "</style>n" 
  130.   . "</head>nn<body>nn" ; 
  131. } 
  132.  
  133.  
  134. function HtmlFoot() 
  135. { 
  136.  return Copyright() . "n</body>nn</html>" ; 
  137. } 
  138.  
  139.  
  140. function Copyright() 
  141. { 
  142.  return "<center><font size="5" face="楷体_GB2312" color="red">使用完请立即删除本文件,以避免被其它人发现使用!</font></center>n" 
  143.   . "<br><hr color="#003388">n" 
  144.   . "<center>n" 
  145.   . "<p style="font-family:verdana; font-size:12px">Contact us: n" 
  146.   . "<a href="http://www.phpfensi.com/" target="_blank">http://www.phpfensi.com/</a></p>n" 
  147.   . "</center>n" 
  148.   . "</body>n" 
  149.   . "n" 
  150.   . "</html>" ; 
  151. } 
  152.  
  153. ?>

Tags: php在线压缩 php文件夹压缩

分享到: