如果您因為某些原因需要一個執行時間恆定的 realpath() 實作,請嘗試
<?php
function realpath_constant_time(string $path, float $target_seconds, bool &$constant_time_success = null){
$start_time=microtime(true);
$ret=realpath($path);
$constant_time_success = @time_sleep_until($start_time+$target_seconds);
return $ret;
}
?>
例如,一個總是恰好使用 1 毫秒的即時函式(對於基於 SSD 的伺服器來說應該綽綽有餘,也許基於旋轉硬碟的伺服器可能需要接近 10 毫秒的時間,我不確定)
<?php
realpath_constant_time("/path/to/../to/file.txt",0.001,$constant_time_success);
?>
您可以使用 $constant_time_success 來查看是否需要更多時間(因此未能以固定時間執行 realpath()),或者是否成功。