由于basename會(huì)過(guò)濾掉/和\,大家看看我這個(gè)解決辦法如何?
將
if(!preg_match($patten,basename($filename))
改為
if(!preg_match($patten,basename($filename))&&pathinfo($filename,PATHINFO_FILENAME)!=''&&pathinfo($filename,PATHINFO_DIRNAME)!='.')
這樣的話(huà)就可以解決問(wèn)題,或者說(shuō)哪位大神有更好的辦法可以說(shuō)說(shuō)呀
2016-06-27
我覺(jué)得你這就挺好的啊
2016-07-30
我將createFile方法改成兩個(gè)參數(shù)的:
1、index.php文件中 ——————————————————————————
if ($act == "創(chuàng)建文件") {
$mes=createFile($filename,$path."/".$filename);
alertMes($mes, $redirect);
}
2、file.func.php文件中 ——————————————————————————
function createFile($filename,$full_filename) {
//file/1.txt
//驗(yàn)證文件名的合法性,是否包含/,*,<>,?,|
$pattern = "/[\/,\*,<>,\?\|]/";
if (! preg_match ( $pattern, $filename ) ) {
//檢測(cè)當(dāng)前目錄下是否存在同名文件
if (! file_exists ( $full_filename )) {
//通過(guò)touch($filename)來(lái)創(chuàng)建
if (touch ( $full_filename )) {
return "文件創(chuàng)建成功";
} else {
return "文件創(chuàng)建失敗";
}
} else {
return "文件名重復(fù)!請(qǐng)重命名后再創(chuàng)建新文件。";
}
} else {
return "非法文件名";
}
}