【发布时间】:2015-06-05 18:36:47
【问题描述】:
我正在尝试在用户上传文件时编写验证代码。 条件:文件大小最大500kb,只有doc和docx文件 我使用此代码但无法正常工作。
我想授予访问者在我的网站上上传简历的权限,并带有服务器端验证 (PHP)。
<html>
<head>
<title>Validation</title>
</head>
<body>
<?php
$msg = "";
$msgsize = "";
if(isset($_POST['add']))
{
$filename=$_FILES['resume']['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
$uname = $_POST['uname'];
$uemail = $_POST['uemail'];
$uphone = $_POST['uphone'];
$resume = $_FILES['resume']['name'];
if($uname=="" || $uemail=="" || $uphone=="" || $resume=="")
{
$msg = "Please fill in all required fields!";
}
else if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$uemail))
{
$msg = "Invalid email format";
}
//echo $ext;
else if($ext != '.doc' or $ext != '.docx')
{
$msg = "Type Error";
}
else if($_FILES["resume"]["size"]>500000)
{
$msg = "Size error". $_FILES["resume"]["size"] . "Only 500KB Resume Allowed";
}
else
{
$msg = "GOOD";
}
}
?>
<div style="background:#FF6600; padding:10px;"><?php echo $msg . $msgsize; ?></div>
<form id="form1" enctype="multipart/form-data" name="form1" method="post" action="<?php $_PHP_SELF ?>">
<table width="700" border="1">
<tr>
<td width="178">Name</td>
<td width="506"><input name="uname" type="text" /></td>
</tr>
<tr>
<td>Email</td>
<td><input name="uemail" type="text" /></td>
</tr>
<tr>
<td>Phone</td>
<td><input name="uphone" type="text" /></td>
</tr>
<tr>
<td>Resume</td>
<td><input name="resume" value="60000000" id="resume" type="file" /></td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" name="add" id="add" value="Add">
</td>
</tr>
</table>
</form>
</body>
</html>
【问题讨论】:
-
发生什么错误..??
-
我想通过所有条件但是这个语句不起作用:else if($ext != '.doc' or $ext != '.docx') 不检查总是错误的结果跨度>
-
当我上传 doc 或 docx 文件时,它会显示 - 仅上传 DOC 或 Docx 文件
-
这样比较
if($ext!='.doc' OR $ext!='.docx' ) -
不工作兄弟检查它并在你的本地主机上运行,得到同样的错误
标签: php validation