0

I am trying to write validation code when user upload file. Condition : file size maximum 500kb and only doc and docx file I use this code but not working properly.

I want to give visitor permission to upload cv in my website, with server side validation (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>
Savan Paun
  • 1,723
  • 2
  • 16
  • 24

3 Answers3

1

here you need to change

else if($ext != '.doc' AND $ext != '.docx') {     //use AND instead of OR
    $msg = "Type Error";
}
Sagar Naliyapara
  • 3,971
  • 5
  • 41
  • 61
1

try this

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

I hope this should work

user789456
  • 173
  • 6
1

strpos() find the first occurance of string inside another string , so if a file name is like this :-

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

so it will not match, you should find occurance of string by method :-

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

it will give you correct ".docx"

some other problems seems :-

using $_FILES['resume'] without checking is that set or not so should be something like this :-

if(!isset($_FILES['resume'])){

       $msg = "File not selected !";  

}

Second

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

shold be something like this :-

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

Thanks :)

Tiger
  • 404
  • 1
  • 4
  • 13