2012-05-18 54 views
0

我已經搜索了其他問題,但找不到可行的解決方案。這是一個CMS程序,我試着將文件上傳到任意目錄,並正在以下錯誤:「警告:preg_replace()[function.preg-replace]:編譯失敗:沒有重複的偏移量3在...」

Warning: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 3 in /home/...on line 1017 

下面是代碼,有什麼建議?:

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; 
     } 
    } 

回答

0

將其更改爲:

$basePath = preg_replace("@/{1,}/@", "/", $_SESSION['rootdirectory'] . '/' . 
$_SESSION["startingdirectory"]); 

preg_replace預計正則表達式一個字符分隔字符串內,這樣就可以像g或指定標誌之後210(我通常使用@)。它將/作爲分隔符。有關更多信息,請參閱preg_replace manual page

即使如此,這個代碼沒有多大意義,並且可能有更好的方法來完成它應該做的事情。

+0

完美。非常感謝@ jnylen。 – csirgo

0
/{1,}/ 

貌似你試圖匹配1個或更多的什麼

相關問題