当前位置:首页 > PHP教程 > php高级应用 > 列表

php文件服务实现虚拟挂载其他目录示例

发布:smiling 来源: PHP粉丝网  添加日期:2020-11-15 20:03:37 浏览: 评论:0 

这篇文章主要介绍了php文件服务实现虚拟挂载其他目录示例,需要的朋友可以参考下,php文件服务实现虚拟挂载其他目录,代码如下:

  1. <?php 
  2. function base64url_encode($data) {  
  3.   return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');  
  4. }  
  5. function base64url_decode($data) {  
  6.   return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));  
  7. } 
  8. function array_verify($var) 
  9.   { 
  10.    return isset($var)&&is_array($var) && count($var)>0; 
  11.   } 
  12.   function format_file_size($var) 
  13.   { 
  14.    if($var<1024) return $var.' B'; 
  15.    if($var<1048576) return ($var/1024.0).' K'; 
  16.    if($var<1073741824) return ($var/1048576.0).' M'; 
  17.    return ($var/1073741824.0).' G'; 
  18.   } 
  19.  
  20.   $dir="F:";//不以/结尾 
  21.   $path=""; 
  22.  
  23.  if(array_verify($_GET)&&isset($_GET["path"])) 
  24.  { 
  25.   $path=base64url_decode($_GET["path"]); 
  26.   preg_match("#^[^/].*$|^.*\.$|^\..*$|\./\.|/\.|\./#",$path,$temp); 
  27.     if(array_verify($temp)) 
  28.    { 
  29.     echo 
  30.       '<html> 
  31.                <head> 
  32.                <meta http-equiv="content-type" content="text/html;charset=gb2312" /> 
  33.                <body>'; 
  34.            echo "警告 index.php?path=".$_GET["path"]." 非法url<br/></body></html>"; 
  35.      exit; 
  36.       } 
  37.  $path=preg_replace("#[/\/]{2,}#","/",$path); 
  38.  } 
  39.  
  40. if(is_dir($dir.$path)) 
  41.     { 
  42.  echo '<html> 
  43.           <head> 
  44.           <meta http-equiv="content-type" content="text/html;charset=gb2312" /> 
  45.           <body>'; 
  46.  echo "目录   <b>".$path."</b><br/><br/>"; 
  47.  
  48.  $dir_res=opendir($dir.$path); 
  49.  while($filen=readdir($dir_res)) 
  50.      { 
  51.       if($filen!='.'&&$filen!='..') 
  52.       { 
  53.           if(is_file($dir.$path.'/'.$filen)) 
  54.           { 
  55.                echo '<a href="index.php?path='.base64url_encode($path.'/'.$filen).'" >'.$filen.'</a> ('.format_file_size(filesize($dir.$path.'/'.$filen)).")<br/>\n"; 
  56.              }else 
  57.            { 
  58.                 echo '<a href="index.php?path='.base64url_encode($path.'/'.$filen).'" >'.$filen."</a><br/>\n"; 
  59.            } 
  60.  
  61.          }else if($filen=='..') 
  62.              { 
  63.                preg_match("#([^/]+/{1})*[^/]+(?=/)#",$path,$parent); 
  64.             if(array_verify($parent)) 
  65.                { 
  66.                        echo '<a href="index.php?path='.base64url_encode('/'.$parent[0]).'" >'.$filen."</a><br/>\n"; 
  67.                   }else 
  68.           { 
  69.                         echo '<a href="index.php?path='.base64url_encode('/').'" >'.$filen."</a><br/>\n"; 
  70.                    } 
  71.           } 
  72.      } 
  73.  echo '</body> 
  74.          </html>'; 
  75.  } 
  76.     else if(is_file($dir.$path)) 
  77.         { 
  78.                 $file_size = filesize($dir.$path); 
  79.                 header("Content-type: application/octet-stream"); 
  80.                 header("Accept-Ranges: bytes"); 
  81.                 header("Accept-Length: ".$file_size); 
  82.                 Header("Content-Disposition: attachment; filename=".basename($dir.$path));  
  83.                 readfile($dir.$path);//大文件请选择其他方式 
  84.            }else 
  85.             echo "警告:非法访问!"; 
  86.  
  87. ?> 

Tags: php文件服务 php虚拟挂载

分享到: