溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php文件服務(wù)如何實現(xiàn)虛擬掛載其他目錄

發(fā)布時間:2021-09-29 17:22:35 來源:億速云 閱讀:109 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)php文件服務(wù)如何實現(xiàn)虛擬掛載其他目錄的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

代碼如下:

<?php
function base64url_encode($data) {
  return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
function base64url_decode($data) {
  return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
}
function array_verify($var)
  {
   return isset($var)&&is_array($var) && count($var)>0;
  }
  function format_file_size($var)
  {
   if($var<1024) return $var.' B';
   if($var<1048576) return ($var/1024.0).' K';
   if($var<1073741824) return ($var/1048576.0).' M';
   return ($var/1073741824.0).' G';
  }

  $dir="F:";//不以/結(jié)尾
  $path="";

 if(array_verify($_GET)&&isset($_GET["path"]))
 {
  $path=base64url_decode($_GET["path"]);
  preg_match("#^[^/].*$|^.*\.$|^\..*$|\./\.|/\.|\./#",$path,$temp);
    if(array_verify($temp))
   {
    echo
      '<html>
               <head>
               <meta http-equiv="content-type" content="text/html;charset=gb2312" />
               <body>';
           echo "警告 index.php?path=".$_GET["path"]." 非法url<br/></body></html>";
     exit;
      }
 $path=preg_replace("#[/\/]{2,}#","/",$path);
 }

if(is_dir($dir.$path))
    {
 echo '<html>
          <head>
          <meta http-equiv="content-type" content="text/html;charset=gb2312" />
          <body>';
 echo "目錄   <b>".$path."</b><br/><br/>";

 $dir_res=opendir($dir.$path);
 while($filen=readdir($dir_res))
     {
      if($filen!='.'&&$filen!='..')
      {
          if(is_file($dir.$path.'/'.$filen))
          {
               echo '<a href="index.php?path='.base64url_encode($path.'/'.$filen).'" >'.$filen.'</a> ('.format_file_size(filesize($dir.$path.'/'.$filen)).")<br/>\n";
             }else
           {
                echo '<a href="index.php?path='.base64url_encode($path.'/'.$filen).'" >'.$filen."</a><br/>\n";
           }

         }else if($filen=='..')
             {
               preg_match("#([^/]+/{1})*[^/]+(?=/)#",$path,$parent);
            if(array_verify($parent))
               {
                       echo '<a href="index.php?path='.base64url_encode('/'.$parent[0]).'" >'.$filen."</a><br/>\n";
                  }else
          {
                        echo '<a href="index.php?path='.base64url_encode('/').'" >'.$filen."</a><br/>\n";
                   }
          }
     }
 echo '</body>
         </html>';
 }
    else if(is_file($dir.$path))
        {
                $file_size = filesize($dir.$path);
                header("Content-type: application/octet-stream");
                header("Accept-Ranges: bytes");
                header("Accept-Length: ".$file_size);
                Header("Content-Disposition: attachment; filename=".basename($dir.$path));
                readfile($dir.$path);//大文件請選擇其他方式
           }else
            echo "警告:非法訪問!";

?>

感謝各位的閱讀!關(guān)于“php文件服務(wù)如何實現(xiàn)虛擬掛載其他目錄”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI