【问题标题】:PHP using for or statement giving errorPHP使用for或语句给出错误
【发布时间】: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>&nbsp;</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


【解决方案1】:

这里你需要改变

else if($ext != '.doc' AND $ext != '.docx') {     //use AND instead of OR
    $msg = "Type Error";
}

【讨论】:

【解决方案2】:

试试这个

$filename=$_FILES['resume']['name'];
if(substr($filename,-3)=='doc' || substr($filename,-4)=='docx'){
    echo 'OK';
}
else{
    echo  "Upload Only DOC or Docx File";
}

我希望这应该工作

【讨论】:

    【解决方案3】:

    strpos() 在另一个字符串中找到第一次出现的字符串,所以如果文件名是这样的:-

    "blahfile-2.2-1.docx" you will get  "2-1.docx" not "docx"
    

    所以它不会匹配,你应该通过方法找到字符串的出现:-

    strrpos()   (case-sensitive) strripos()  (case-insensitive)
    

    它会给你正确的“.docx”

    似乎还有一些其他问题:-

    使用 $_FILES['resume'] 而不检查是否设置 所以应该是这样的:-

    if(!isset($_FILES['resume'])){
    
           $msg = "File not selected !";  
    
    }
    

    第二

    $uname = $_POST['uname'];
    $uemail = $_POST['uemail'];
    $uphone = $_POST['uphone'];   
    $resume = $_FILES['resume']['name'];
    

    应该是这样的:-

    $uname = (isset($_POST['uname']) ? $_POST['uname'] : "";
    $uemail = ...
    $uphone = ...   
    $resume = ...
    

    谢谢:)

    【讨论】:

      猜你喜欢
      • 2018-12-03
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-10
      • 1970-01-01
      相关资源
      最近更新 更多