php web下载网站代码

PHP代码
  1. <?php  
  2. /** 
  3.  * @author MarkZhao(zhaody901#126.com) 
  4.  */  
  5. $dir = dirname(__FILE__).'/';  
  6. // zip and download  
  7. // zip.php?type=zip&dir=subdir  
  8.   
  9. // unzip deflate way zipped zip file  
  10. // zip.php?type=unzip&name=deflate  
  11.   
  12. $type = $_GET['type'];  
  13. $type or exit('die no type to do!');  
  14. $name = isset($_GET['name']) ? $_GET['name'].'.zip' : 'zip.zip';  
  15. $subdir = $_GET['dir'];  
  16. $subdir or die('no sub dir to zip');  
  17. set_time_limit(0);  
  18. if ($type=='zip') {  
  19.     $zip = new ZipArchive();  
  20.     if ($zip->open($name, ZipArchive::Create) === TRUE) {  
  21.         $files = map($dir.$subdir);  
  22.         foreach ($files as $file) {  
  23.             $dir  = str_replace('\\', '/', $dir); 
  24.             $file = str_replace('\\', '/', $file); 
  25.             $zip->addFile($file, '_ROOT_/' . str_replace($dir, '', $file)); 
  26.         } 
  27.         $zip->close(); 
  28.         down($dir.$name); 
  29.         echo 'ok'; 
  30.     } else { 
  31.         echo 'failed'; 
  32.     } 
  33. } else if ($type=='unzip') { 
  34.  
  35.     $zip = new ZipArchive(); 
  36.     if ($zip->open($name) === TRUE) { 
  37.         $zip->extractTo(dirname(__FILE__).'/'); 
  38.         $zip->close(); 
  39.         echo 'ok'; 
  40.     } else { 
  41.         echo 'failed'; 
  42.     } 
  43. } 
  44.  
  45.  
  46. function map($directory) { 
  47.     $files = array(); 
  48.     if (file_exists($directory) && is_dir($directory)) { 
  49.         foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) { 
  50.             $files[] = $file->getRealPath(); 
  51.         } 
  52.     } 
  53.     return $files; 
  54. } 
  55.  
  56. function down($file) { 
  57.     $filename = $filename ? $filename : basename($file); 
  58.     $filesize = filesize($file); 
  59.     ob_end_clean(); 
  60.     @set_time_limit(0); 
  61.     if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) { 
  62.         header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
  63.         header('Pragma: public'); 
  64.     } else { 
  65.         header('Pragma: no-cache'); 
  66.     } 
  67.     header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT'); 
  68.     header('Content-Encoding: none'); 
  69.     header('Content-Length: ' . $filesize); 
  70.     header('Content-Disposition: attachment; filename=' . $filename); 
  71.     header('Content-Type: ' . $filetype);  
  72.     readfile($file);  
  73.     exit;  
  74. }  

 

1. 打包代码, 并直接通过浏览器下载
2. 解压zip文件到服务器



上一篇: php合成或者创建gif动画
下一篇: PHP利用CURL进行POST数据上传
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: php
相关日志:
评论: 0 | 引用: 0 | 查看次数: 2938
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭

 广告位

↑返回顶部↑