Home > Backend Development > PHP Tutorial > PHP file service, 'virtual mount' other directories

PHP file service, 'virtual mount' other directories

WBOY
Release: 2016-07-25 08:48:16
Original
811 people have browsed it
暂不支持,断点下载.
  1. function base64url_encode($data) {
  2. return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
  3. }
  4. function base64url_decode($data) {
  5. return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
  6. }
  7. function array_verify($var)
  8. {
  9. return isset($var)&&is_array($var) && count($var)>0;
  10. }
  11. function format_file_size($var)
  12. {
  13. if($var<1024) return $var.' B';
  14. if($var<1048576) return ($var/1024.0).' K';
  15. if($var<1073741824) return ($var/1048576.0).' M';
  16. return ($var/1073741824.0).' G';
  17. }
  18. $dir="F:";//不以/结尾
  19. $path="";
  20. if(array_verify($_GET)&&isset($_GET["path"]))
  21. {
  22. $path=base64url_decode($_GET["path"]);
  23. preg_match("#^[^/].*$|^.*.$|^..*$|./.|/.|./#",$path,$temp);
  24. if(array_verify($temp))
  25. {
  26. echo
  27. '
  28. ';
  29. echo "警告 index.php?path=".$_GET["path"]." 非法url
    ";
  30. exit;
  31. }
  32. $path=preg_replace("#[//]{2,}#","/",$path);
  33. }
  34. if(is_dir($dir.$path))
  35. {
  36. echo '
  37. ';
  38. echo "目录 ".$path."

    ";
  39. $dir_res=opendir($dir.$path);
  40. while($filen=readdir($dir_res))
  41. {
  42. if($filen!='.'&&$filen!='..')
  43. {
  44. if(is_file($dir.$path.'/'.$filen))
  45. {
  46. echo ''.$filen.' ('.format_file_size(filesize($dir.$path.'/'.$filen)).")
    n";
  47. }else
  48. {
  49. echo ''.$filen."
    n";
  50. }
  51. }else if($filen=='..')
  52. {
  53. preg_match("#([^/]+/小左)*[^/]+(?=/)#",$path,$parent);
  54. if(array_verify($parent))
  55. {
  56. echo ''.$filen."
    n";
  57. }else
  58. {
  59. echo ''.$filen."
    n";
  60. }
  61. }
  62. }
  63. echo '
  64. ';
  65. }
  66. else if(is_file($dir.$path))
  67. {
  68. $file_size = filesize($dir.$path);
  69. header("Content-type: application/octet-stream");
  70. header("Accept-Ranges: bytes");
  71. header("Accept-Length: ".$file_size);
  72. Header("Content-Disposition: attachment; filename=".basename($dir.$path));
  73. readfile($dir.$path);//大文件请选择其他方式
  74. }else
  75. echo "警告:非法访问!";
  76. ?>
复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template