警告:僅使用此解決方案緊急搶修,NEVER較長的生產代碼
定義與重寫方法的類,看到http://php.net/manual/en/class.streamwrapper.php
<?php
class YourEmergencyWrapper {
static $from = '/home/clientx/htdocs/';
static $to = 'c:/shared/clients/client';
private $resource = null;
//...some example stream_* functions, be sure to implement them all
function stream_open($path,$mode,$options=null,&$opened_path){
$path = self::rewrite($path);
self::restore();
$this->resource = fopen($path,$mode,$options);
self::reenable();
$opened_path = $path;
return is_resource($this->resource);
}
function stream_read($count){
self::restore();
$ret = fread($this->resource,$count);
self::reenable();
return $ret;
}
function stream_eof(){
self::restore();
$ret = feof($this->resource);
self::reenable();
return $ret;
}
function stream_stat(){
self::restore();
$ret = fstat($this->resource);
self::reenable();
return $ret;
}
static function rewrite($path){
if(strpos($path,self::$from)===0) $path = self::$to.substr($path,strlen(self::$from));
return $path;
}
//... other functions
private static function restore(){
stream_wrapper_restore('file');
}
private static function reenable(){
stream_wrapper_unregister('file');
stream_wrapper_register('file',__CLASS__);
}
}
stream_wrapper_unregister('file');
stream_wrapper_register('file','YourEmergencyWrapper');
嚴重的是,只在你自己的開發服務器上進行一些本地調試。幾乎所有的代碼都可以強制它作爲auto_prepend。留下一些功能還有待執行; P
在這種情況下,對主代碼的更改不是一種選擇 - 我是一個承包商,負責代碼的公司也是如此。我與客戶端有「政治資本」,我可以用它來強制更改,但我只會將其用於諸如安全漏洞之類的重要事情。 – justkevin 2010-09-28 15:37:27