php檔案服務實現虛擬掛載其他目錄樣本_PHP教程

來源:互聯網
上載者:User
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:";//不以/結尾
$path="";

if(array_verify($_GET)&&isset($_GET["path"]))
{
$path=base64url_decode($_GET["path"]);
preg_match("#^[^/].*$|^.*\.$|^\..*$|\./\.|/\.|\./#",$path,$temp);
if(array_verify($temp))
{
echo
'


';
echo "警告 index.php?path=".$_GET["path"]." 非法url
";
exit;
}
$path=preg_replace("#[/\/]{2,}#","/",$path);
}

if(is_dir($dir.$path))
{
echo '


';
echo "目錄 ".$path."

";

$dir_res=opendir($dir.$path);
while($filen=readdir($dir_res))
{
if($filen!='.'&&$filen!='..')
{
if(is_file($dir.$path.'/'.$filen))
{
echo ''.$filen.' ('.format_file_size(filesize($dir.$path.'/'.$filen)).")
\n";
}else
{
echo ''.$filen."
\n";
}

}else if($filen=='..')
{
preg_match("#([^/]+/{1})*[^/]+(?=/)#",$path,$parent);
if(array_verify($parent))
{
echo ''.$filen."
\n";
}else
{
echo ''.$filen."
\n";
}
}
}
echo '
';
}
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 "警告:非法訪問!";

?>

http://www.bkjia.com/PHPjc/755842.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/755842.htmlTechArticlephp檔案服務實現虛擬掛載其他目錄 複製代碼 代碼如下: ?php function base64url_encode($data) { return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); } funct...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.