分开来写,也就是要两条正则表达式,一条验证目录路径,一条验证文件全路径。
具体要求如下:验证目录路径的,可以这样想,把目录路径分成三部分:
一,开始有一个盘符名称和一个冒号[a-zA-Z]: 这部分只会出现一次,必须有。例如 D:
二,然后是Windows的路径分隔符"\"和目录名 "/"认为是不合法 分隔符在正则表达式里是\\,目录名是至少一个的字母、数字、下划线,中间可以有0个或者多个空格 这部分可以出现0次或者多次。例如 \demo 或者\demo\program files
三,最后还是一个路径分割符"\" 这部分只可以出现0次或者1次 因此, D:\ D:\demo D:\demo\program files D:\demo\program files\ 都算合法的
验证文件全路径的,就是除了以上的目录路径外,最后文件扩展名任意,系统支持的文件名都可以。自己试了好久,总是不对,望各位帮忙瞅瞅,谢谢
具体要求如下:验证目录路径的,可以这样想,把目录路径分成三部分:
一,开始有一个盘符名称和一个冒号[a-zA-Z]: 这部分只会出现一次,必须有。例如 D:
二,然后是Windows的路径分隔符"\"和目录名 "/"认为是不合法 分隔符在正则表达式里是\\,目录名是至少一个的字母、数字、下划线,中间可以有0个或者多个空格 这部分可以出现0次或者多次。例如 \demo 或者\demo\program files
三,最后还是一个路径分割符"\" 这部分只可以出现0次或者1次 因此, D:\ D:\demo D:\demo\program files D:\demo\program files\ 都算合法的
验证文件全路径的,就是除了以上的目录路径外,最后文件扩展名任意,系统支持的文件名都可以。自己试了好久,总是不对,望各位帮忙瞅瞅,谢谢
<?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!";
}
?>
http://www.chinaltesummit.com/apps/xx.php
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!";
}
?>
比如说,你上面那个,用来验证路径,能否帮忙把文件也加进去呢??
比如:
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你的意思是只有目录不行必须有文件吗?
轮子已经有了就别再造了
就是我客户端输入了一个路径,如下
c:\windows\system32但是在PHP代码中,得到的却是c:\\windwos\\system32,保存到数据库后就成这种了,哪位知道这个问题是怎么回事???
可不可以发一个用filter_var验证Windows路径+文件的例子,学习一下。
<!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!";
}
?>
提交后 如果开启了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