发送邮件错误并上传错误

时间:2017-04-11 08:12:31

标签: php file-upload

我创建了一个表单,它工作正常,但我有两个问题:

  1. 我收到此错误消息:
  2.   

    警告:mail(/var/log/phpmail.log):无法打开流:权限   在第115行的/www1/xxx/Contact_Us.php中被拒绝。

    1. 我无法上传大于20KB的文件,即使在我的代码中我允许直到2 MB,而php.ini upload_max_filesize上的文件是8MB且post_max_size是8MB。
    2. 所以,任何人都可以帮我解决这两个问题吗?

      <?php
      //set tiem zone
      date_default_timezone_set('Asia/Dubai');
      // Show errors
      error_reporting(E_ALL);
      ini_set('display_errors', 1);
      
      // define Errors variables
      $fnameErr = $lnameErr = $emailErr = $humanErr = $fileErr = $result = $result2 =  "" ;
      
      // when we press submit do the following
      if(isset($_POST['submit']))
      {
      // define contact form variables
      $fname = $_POST['fname'];
      $lname = $_POST['lname'];
      $email = $_POST['email'];
      $tel = $_POST['tel'];
      $design = $_POST['design'];
      $country = $_POST['country'];
      $comment = $_POST['comment'];
      $human = $_POST['human'];
      
      // define Checks variables
      $check1 = $check2 = $check3  = $check4  = "";
      
      
      // Let's do some checks 
      // Checking the First Name
      if(empty($_POST["fname"])){
          $fnameErr = "Name is Required";
      }else{
          $fname = test_input($_POST["fname"]);
          // check if name only contains letters and whitespace
          if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
            $fnameErr = "Only letters and white space allowed"; 
          }else{
              $check1 = 1;
          }
      }
      // Checking the Last Name   
      if(empty($_POST["lname"])){
          $lnameErr = "Name is Required";
      }else{
          $lname = test_input($_POST["lname"]);
          // check if name only contains letters and whitespace
          if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
            $lnameErr = "Only letters and white space allowed"; 
          }else{
              $check2 = 1;
          }
      }
      //Checking the Email Adress
      if(empty($_POST["email"])){
          $emailErr = "Email is Required";
      }else{
          $email = test_input($_POST["email"]);
          // check if e-mail address is well-formed
          if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "Invalid email format"; 
          }else{
              $check3 = 1;
          }
      }
      //Checking the Anti-Spam Question
      if(empty($_POST["human"])){
          $humanErr = "Please Enter the Answer";
      }else{
          if ($human != 4){
              $humanErr = "Please check your answer";
          }else{
              $check4 = 1;
          }
      }
      
      // checking the attachment
      if(isset($_FILES) && (bool) $_FILES) {
      
          $allowedExtensions = array("pdf","doc","docx");
      
          $files = array();
          foreach($_FILES as $name=>$file) {
              $file_name = $file['name']; 
              $temp_name = $file['tmp_name'];
              $file_type = $file['type'];
              $file_size = $file['size'];
              $path_parts = pathinfo($file_name);
              $ext = $path_parts['extension'];
      
              if($file_size > 2000000){
              $fileErr = "Max allowed size is 2 MB";
              } else {
                      if(!in_array($ext,$allowedExtensions)) {
                      $fileErr = "File $file_name has the extensions $ext which is not allowed";    
                      }else{
                          array_push($files,$file);}
      
                     }
          }
      
      
      // define email variables
      $to = 'email@domain.com';
      $from = "Job Application"; 
      $subject = 'Job Application';
      $message = 'From: '.$fname .$lname."\r\n".
                 'E-mail: '.$email."\r\n".
                 'Telephone: '.$tel."\r\n".
                 'Designation: '.$design."\r\n".
                 'Country Appled From: '.$country."\r\n".
                 'Message: '.$comment."\r\n"."\r\n";
      $headers = "From: $from";   
      // boundary 
          $semi_rand = md5(time()); 
          $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
      
          // headers for attachment 
          $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
      
          // multipart boundary 
          $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
          $message .= "--{$mime_boundary}\n";
      
          // preparing attachments
      
          for($x=0;$x<count($files);$x++){
              $file = fopen($files[$x]['tmp_name'],"rb");
              $data = fread($file,filesize($files[$x]['tmp_name']));
              fclose($file);
              $data = chunk_split(base64_encode($data));
              $name = $files[$x]['name'];
              $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" . 
              "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" . 
              "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
              $message .= "--{$mime_boundary}\n";
          }   
      
      
      // Emailing the Contents if all Checks are correct  
      if($check1 && $check2 && $check3 && $check4  == 1){
          mail($to, $subject, $message, $headers);
          $result =  "Message Sent Sucessfully";
      }else{
          $result2 = "Message Can't be sent";
      }
      } }
       function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
      }
      
      ?>
      

0 个答案:

没有答案