1

他の質問を検索しましたが、実行可能な解決策が見つかりません。これはCMSプログラムです。任意のディレクトリにファイルをアップロードしようとしましたが、次のエラーが発生します。

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

2 に答える 2

0

次のように変更します。

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

preg_replace正規表現が文字列内の文字で区切られていることを想定しているため、のようなフラグを指定しgたり、i後でフラグを指定したりできます(私は通常使用します@)。/区切り文字として扱っていました。詳細については、preg_replaceマニュアルページを参照してください。

それでも、このコードはあまり意味がなく、おそらく、本来の目的を達成するためのはるかに優れた方法があります。

于 2012-05-18T13:33:28.810 に答える
0
/{1,}/

1つ以上の何も一致させようとしているようです。

于 2012-05-18T13:33:29.050 に答える