-1

I create a PHP contact form with file attachment with helping a online tutorial. But i want to function it like user only upload .txt .doc .docx file format, no other format will be accepted.

<?php $from = "[email protected]"; $to = $_POST['email']; $subject =$_POST['subject']; $message = $_POST['body']; // Temporary paths of selected files $file1 = $_FILES['file1']['tmp_name']; $file2 = $_FILES['file2']['tmp_name']; $file3 = $_FILES['file3']['tmp_name']; // File names of selected files $filename1 = $_FILES['file1']['name']; $filename2 = $_FILES['file2']['name']; $filename3 = $_FILES['file3']['name']; // array of filenames to be as attachments $files = array($file1, $file2, $file3); $filenames = array($filename1, $filename2, $filename3); // include the from email in the headers $headers = "From: $from"; // boundary $time = md5(time()); $boundary = "==Multipart_Boundary_x{$time}x"; // headers used for send attachment with email $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$boundary}\""; // multipart boundary $message = "--{$boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; $message .= "--{$boundary}\n"; // attach the attachments to the message for($x=0; $x<count($files); $x++){ $file = fopen($files[$x],"r"); $content = fread($file,filesize($files[$x])); fclose($file); $content = chunk_split(base64_encode($content)); $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . "Content-Disposition: attachment;\n" . " filename=\"$filenames[$x]\"\n" . "Content-Transfer-Encoding: base64\n\n" . $content . "\n\n"; $message .= "--{$boundary}\n"; } // sending mail $sendmail = mail($to, $subject, $message, $headers); // verify if mail is sent or not if ($sendmail) { echo "Sent successfully!"; } else { echo "Error occurred. Try again!"; } ?> 
2
  • 1
    Are you trying to prevent people from accidentally sending files that wouldn't be helpful or are you trying to prevent malicious users from spreading viruses? Commented Oct 15, 2013 at 18:32
  • See this answer on SO it will give you a good idea on how to do this. Although I'm not keen on using W3Schools as a reference, you can see this page under the Restrictions on Upload section Commented Oct 15, 2013 at 18:43

4 Answers 4

1

the use of $_FILE["filename"]["type"] is not recomended for file type checking as the type parameter is browser specific i.e different browser can have different type So we will try to extract the format from the string function

$filename=$_FILES["file1"]["name"];//get the name of the file $extension=strrchr($filename, ".");//extracting the extension if($extension=".txt" || $extension=".doc" || $extension=".docx") { //send mail; } else { //error; } 
Sign up to request clarification or add additional context in comments.

1 Comment

old, but this does not work, nor are the if statement operators correct.
1

Simply,

$filename_array = explode(".",$filename); $ext = ".".$filename_array[count($filename_array)-1]; if($ext!==".txt" && $ext!==".doc" && $ext!==".docx"): //Do bad extension code endif; //Do code that passed extension validation 

Hope this helps!

Comments

1
$allowed = array('.txt', '.doc', '.docx') $fileNames = array(); $filename1= $_FILES['file1']['name']; $ext1 = pathinfo($path, PATHINFO_EXTENSION); if(in_array($ext1, $allowed)){ array_push($fileNames, $filename1); //repeat for 2, 3, etc... } 

Comments

-1
function allowed_file(){ //Add the allowed mime-type files to an 'allowed' array $allowed = array('application/doc', 'application/txt', 'another/type'); //Check uploaded file type is in the above array (therefore valid) if(in_array($_FILES['file']['name'], $allowed)) { // Your code } } 

Reference : php file upload, how to restrict file upload type

1 Comment

First, this code would not run. Second, what would it achieve if it did?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.