"Warning: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 3 in..."
我搜索了其他问题,但找不到可行的解决方案。这是一个 CMS 程序,我尝试将文件上传到任何目录并收到以下错误:
1 | Warning: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 3 in /home/...on line 1017 |
这是代码,有什么建议吗?:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | function _IsValidPath($Path) { // First check if the path starts with the starting directory $basePath = preg_replace("/{1,}/","/", $_SESSION['rootdirectory'] . '/' . // this line (1017) causing the error $_SESSION["startingdirectory"]); $sdPos = strpos($basePath, $Path); if (!is_numeric($sdPos)) { $sdPos = strpos($Path, $basePath); } if(is_numeric($sdPos) && $sdPos == 0) { // Make sure it doesn't contain any invalid characters if(is_numeric(strpos($Path,"..")) || (is_numeric(strpos($Path,"./"))) || (is_numeric(strpos($Path,"//"))) || (is_numeric(strpos($Path,"\"))) || (is_numeric(strpos($Path,"../"))) || (is_numeric(strpos($Path,"&"))) || (is_numeric(strpos($Path,"*"))) || (is_numeric(strpos($Path,""))) || (is_numeric(strpos($Path,"'"))) || (is_numeric(strpos($Path,"\\?"))) || (is_numeric(strpos($Path,"<"))) || (is_numeric(strpos($Path,">")))) { return false; } else { // The path is OK return true; } } else { return false; } } |
1 | /{1,}/ |
看起来您正在尝试匹配 1 个或多个无内容。
改成:
1 2 | $basePath = preg_replace("@/{1,}/@","/", $_SESSION['rootdirectory'] . '/' . $_SESSION["startingdirectory"]); |
即使如此,这段代码也没有多大意义,而且可能有更好的方法来完成它应该做的事情。