<?php
$path="file";
$act=$_REQUEST['act'];
$filename=$_REQUEST['filename'];
$info=readDirectory($path);
//print_r($info);
$redirect="index.php?path={$path}";
if($act=="createFile"){
$mes=createFile($path."/".$filename);
alertMes($mes,$redirect);
}
function createFile($filename){
//验证文件的合法性是否包含/,*,<>,?,|
$pattern="/[\/,\*,<>,\?\|]/";
if (!preg_match($pattern,basename($filename))){
//检测当前目录下是否存在同名文件
if (!file_exists($filename)){
//通过touch($filename)来创建
if (touch($filename)){
return "文件创建成功!";
}else{
return " 文件创建失败!";
}
}else {
return "文件已存在,请重命名后创建!";
}
}else {
return "非法文件名";
}
}
function alertMes($mes,$url) {
echo "<script type='text/javascript'> alert('{$mes}');location.href='{$url}'; </script> ";
}
<td>请输入文件名称</td>
<td><input type="text" name="filename" />
<input type="hidden" name="path" value="<?php echo $path?>" />
<input type="hidden" name="act" value="createFile" />
<input type="submit" value="创建文件" />
</td>
<form action="index.php" method="psot" enctype="multipart/form-data">
...........
</form>
表单form你都没有写