PHP File Service Implementation virtual mount other directory
Copy Code code as follows:
<?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:";//Not AT/end
$path = "";
if (array_verify ($_get) &&isset ($_get["path"))
{
$path =base64url_decode ($_get["path"]);
Preg_match ("#^[^/].*$|^.*\.$|^\.") *$|\./\.| /\.| \./# ", $path, $temp);
if (Array_verify ($temp))
{
Echo
' <meta http-equiv= "Content-type" content= "text/html;charset=gb2312"/>
<body> ';
echo "Warning Index.php?path=". $_get["path". "Illegal url<br/></body>Exit
}
$path =preg_replace ("#[/\/]{2,}#", "/", $path);
}
if (Is_dir ($dir. $path))
{
Echo ' <meta http-equiv= "Content-type" content= "text/html;charset=gb2312"/>
<body> ';
echo "Directory <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>
}
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);//Large file please choose another way
}else
echo "WARNING: illegal access!";
?>