Question

I'm attempting to write this code for an where it can play as an application form and submits the information entered by the user through PHP mail. But I continue to get this error where it states that the file type is not allowed.

Some of the reserved words are not highlighted, so I'm not sure if the program doesn't recognized their functions. I checked the basename function, where basename is not highlighted as it should be. So I tried to use pathinfo, but I don't want the whole path just the the basename of it. Is it because it is not encoding correctly so it can't recognize the file type?? Please check my check my code.....

<?php

if(!empty($_FILES['resume_name']['name'])){

    $file_name=$_FILES['resume_name']['name'];
    $temp_name=$_FILES['resume_name']['tmp_name'];
    $file_type=$_FILES['resume_name']['type'];
    $file_size=$_FILES['resume_name']['size'];

    $base=basename($file_name);
    $extension= pathinfo($base, PATHINFO_EXTENSION);

    $allowed_ext=array(".doc",".docx",".pdf",".zip",".jpeg",".jpg",".txt");

    if (array_intersect($extension,$allowed_ext)){
    $from=$_POST['email'];
    $to="travis.dacosta1@gmail.com";
    $subject= $_POST['departments'];
    $message=' 


            <table cellspacing="0" cellpadding="8" border="0" width="400"> 
            <tr> 
                <td colspan="2"></td> 
            </tr> 
            <tr bgcolor="#eeeeee"> 
                <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Name</strong></td> 
                <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$first_name.'</td> 
            </tr>  
            <tr bgcolor="#eeeeee"> 
                <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Name</strong></td> 
                <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$last_name.'</td> 
            </tr>  
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr> 
            <tr bgcolor="#eeeeee"> 
                <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Email</strong></td> 
                <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$email.'</td> 
            </tr> 

            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr> 
                <tr bgcolor="#eeeeee"> 
                <td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Departments</strong></td> 
            </tr>                
            <tr bgcolor="#eeeeee"> 
                <td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$departments.'</td> 
            </tr>           

            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr> 

            <tr bgcolor="#eeeeee"> 
                <td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Qualifications</strong></td> 
            </tr>                
            <tr bgcolor="#eeeeee"> 
                <td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$qualifications.'</td> 
            </tr>                

            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr> 
         </table> 

';

    $file=$temp_name;
    $content = chunk_split(base64_encode(file_get_contents($file)));
    $uid=md5(uniqid(time()));

    $header = "From:    ". $from. "\r\n";
    $header .= "Reply-To:   ". $replyto. "\r\n";
    $header .= "MIME-Version: 1.0\r\n";

    $header .="Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";


    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";

    $header .= "--".$uid."\r\n";
    $header .= "Content-type:   ".$file_type."; name=\"".$file_name."\"\r\n";
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: resume_name; filename=\"".$file_name."\"";
    $header .= $content."\r\n\r\n";

    if(@mail($to, $subject, $message, $header)){
            echo "Success";
            }
    else{
        echo "Fail";
        }
    }
else {
    echo "File Type Not Allowed!!";
     }
}
else{    
    echo "No File Posted!!";
    }
}
exit();
?>
Was it helpful?

Solution

array_intersect takes an array as it first parameters. pathinfo($base, PATHINFO_EXTENSION); gives a String, not an Array.

You can try in_array:

if (in_array($extension, $allowed_ext)) {
    ....
}

Also, your $allowed_ext should be without dots:

$allowed_ext = array("doc", "docx", "pdf", "zip", "jpeg", "jpg", "txt");

because pathinfo($base, PATHINFO_EXTENSION); gives the extension without the dot.

http://www.php.net/manual/en/function.pathinfo.php

OTHER TIPS

Email attachment won't allow certain types because you already specified some file extensions in your code.So you may not allow other than these file extensions.

Also remove dots from your file extension You can add more file extensions here.

$allowed_ext=array("doc","docx","pdf","zip","jpeg","jpg","txt");
    $allowed_ext=array("doc","docx","pdf","zip","jpeg","jpg","txt");


remove dot in the extence

I actually figured out what was wrong. You all will laugh but, I was working on the same document but from a different directory. So the revisions I was making wasn't being uploaded to the server. After taking all of your suggestions it was able to upload the file perfectly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top