PHP远程文件镜像,运用rewrite可以做到完全无缝cache~~


  1. <?php
  2. /**
  3. .htaccess
  4. RewriteEngine on
  5. RewriteCond %{SCRIPT_FILENAME} !-f
  6. RewriteCond %{SCRIPT_FILENAME} !-d
  7. RewriteRule ^(.*)$ cache.php
  8. */
  9.  
  10. include "mime.php";
  11. function mkdir_r($pathname, $mode)
  12. {
  13.     is_dir(dirname($pathname)) || mkdir_r(dirname($pathname), $mode);
  14.     return is_dir($pathname) || @mkdir($pathname, $mode);
  15. }
  16. $base=dirname(__FILE__); //这个文件叫做cache.php 放到跟下,要不会出问题哦~~自己看看路径吧~~
  17. $host=‘http://www.fabrichina.net’; //looking up Host
  18. $test=file_get_contents($host.$_SERVER[‘REQUEST_URI’]);
  19. $file=explode(‘/’,$_SERVER[‘REQUEST_URI’]);
  20. $fileName=array_pop($file);
  21. $filePath=$base.‘/’.implode(‘/’,$file).‘/’;
  22. mkdir_r($filePath,0777);
  23. file_put_contents($filePath.$fileName,$test);
  24. $ext=explode(‘.’,$_SERVER[‘REQUEST_URI’]);
  25. $ext=array_pop($ext);
  26. if(isset($mime[$ext]))
  27.  header(‘Content-Type: ‘.$mime[$ext]);
  28. header(‘Content-Length: ‘.strlen($test));
  29. echo $test;
  30.  
  1. No comments yet.
(will not be published)
  1. No trackbacks yet.