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

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

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