分开来写,也就是要两条正则表达式,一条验证目录路径,一条验证文件全路径。
具体要求如下:验证目录路径的,可以这样想,把目录路径分成三部分:
  一,开始有一个盘符名称和一个冒号[a-zA-Z]: 这部分只会出现一次,必须有。例如 D:   
  二,然后是Windows的路径分隔符"\"和目录名 "/"认为是不合法 分隔符在正则表达式里是\\,目录名是至少一个的字母、数字、下划线,中间可以有0个或者多个空格 这部分可以出现0次或者多次。例如 \demo 或者\demo\program files
  三,最后还是一个路径分割符"\" 这部分只可以出现0次或者1次   因此, D:\  D:\demo  D:\demo\program files  D:\demo\program files\  都算合法的
验证文件全路径的,就是除了以上的目录路径外,最后文件扩展名任意,系统支持的文件名都可以。自己试了好久,总是不对,望各位帮忙瞅瞅,谢谢   

解决方案 »

  1.   

    就是使用preg_match()函数,来验证客户端提交的数据,但是这个方法里面的正则,我测试了好多个,都是不行,想不明白为什么
      

  2.   

    需要 使用'/([])\/([])/i'这样的形式吧?其实感觉 可以使用ereg
      

  3.   


    <?php
    function check_path($path)
    {
    $match="^[a-zA-Z]{1}:[ A-Za-z0-9\\_-]+$";
    if(ereg($match,$path))return true;
    else return false;
    }$str="x:\\abcdassadas das\\asdas12d44_aa-sxXA0Sd\\";
    //$str="D:\\";
    //$str="D:\\demo";
    //$str="D:\\demo\\program files";
    //$str="D:\\demo\\program files\\";
    if(check_path($str))

    echo "Success!";

    else

    echo "Please try again!";

    ?>
      

  4.   

    测试地址
    http://www.chinaltesummit.com/apps/xx.php
      

  5.   

    修改了一下  没看到还要加文件名
    function check_path($path)
    {
    $match="^[a-zA-Z]{1}:[ \.A-Za-z0-9\\_-]+[ \.A-Za-z0-9_-]+$";
    if(ereg($match,$path))return true;
    else return false;
    }//$str="x:\\abcdassadas das\\asdas12d44_aa-sxXA0Sd\\";
    //$str="D:\\";
    //$str="D:\\demo";
    //$str="D:\\demo\\program files";
    //$str="D:\\demo\\program files\\";
    if(count($_POST)==0)exit;
    $str=$_POST['path'];
    if(check_path($str))

    echo "Success!";

    else

    echo "Please try again!";

    ?>
      

  6.   

    还需要一个正则,,希望futurecs大虾能帮忙下,就是文件路径,要求是这样的 ,任何后缀的都可以通过,但是要求是文件的全路径
    比如说,你上面那个,用来验证路径,能否帮忙把文件也加进去呢??
    比如:
    c:\demo\tmp\aa.txt(文件后缀不限制)
    谢谢啦
      

  7.   

    刚才那个已经可以验证 c:\demo\tmp\aa.txt 这样的文件了
    function check_path($path)
    {
        $match="^[a-zA-Z]{1}:[ \.A-Za-z0-9\\_-]+[ \.A-Za-z0-9_-]+$";
        if(ereg($match,$path))return true;
        else return false;
    }
    测试地址http://www.chinaltesummit.com/apps/xx.php你的意思是只有目录不行必须有文件吗?
      

  8.   

    为何不用php内置的函数filter_var呢?
    轮子已经有了就别再造了
      

  9.   

    楼上这位说的不错,但是那个不太符合我的实例需求啊。顺便说一个问题,,不知道哪位清楚怎么回事
    就是我客户端输入了一个路径,如下
    c:\windows\system32但是在PHP代码中,得到的却是c:\\windwos\\system32,保存到数据库后就成这种了,哪位知道这个问题是怎么回事???
      

  10.   

    php的magic_quotes_gpc 默认是开启的,会自动对提交的数据中的特殊字符进行转义。在确保变量中的字符不会对数据库造成威胁的话 在存入数据库前调用stripslashes(string)函数即可
      

  11.   


    可不可以发一个用filter_var验证Windows路径+文件的例子,学习一下。
      

  12.   

    我加了一个自动反转义的,测试地址http://www.chinaltesummit.com/apps/xxx.php
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head><body>
    <form id="form1" name="form1" method="post" action="xxx.php">
    <input name="path" type="text" size="100" /><br />
    <input type="submit" value="submit" />
    </form>
    </body>
    </html>
    <?php
    function check_path($path)
    {
    $match="^[a-zA-Z]{1}:([\\]{1}[ A-Za-z0-9_-]+)*[\\]{1}[ A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$";
    if(ereg($match,$path))return true;
    else return false;
    }if(count($_POST)==0)exit;
    $str=$_POST['path'];
    if(get_magic_quotes_gpc())$str=stripslashes($str); //这里检测magic_quotes_gpc是否开启,开启的话自动反转义。
    if(check_path($str))

    echo "Success!";

    else

    echo "Please try again!";

    ?>
      

  13.   

    呵呵。。我找了下,,,只有验证IP,Email的。没见有路径的。
      

  14.   

    你在网页里输入 C:\windows\system32\cmd.exe 这样的路径 
    提交后 如果开启了magic_quotes_gpc 就会自动变成 C:\\windows\\system32\\cmd.exe这样用mysql_query存入数据库是不会出问题的,存入后就是C:\windows\system32\cmd.exe如果你在mysql_query中输入的是C:\windows\system32\cmd.exe
    存入后就是C:windowssystem32cmd.exe