2017-04-07 7 views
0

このコードは正常に実行されています。これを解決するには、png JPEGのようなイメージタイプのみを使用してください。エラーまたは成功のメッセージが同じページに表示されている場合すべてのメールが迷惑メールボックスに入ります。これに関しては助けてください。成功メッセージを同じページにエコーしてイメージタイプファイルをアップロードする方法

<form enctype="multipart/form-data" method="POST" action=""> 
    <label>Your Profile Pic<input type="file" name="my_file" /></label> 
    <label><input type="submit" name="button" value="Submit" /></label> 
</form> 
<?php 
if($_POST && isset($_FILES['my_file'])) { 

    $from_email = '[email protected]'; //from mail, it is mandatory with some hosts 
    $recipient_email = '[email protected]'; //recipient email (most cases it is your personal email) 

    //Capture POST data from HTML form and Sanitize them, 
    //$sender_name = filter_var($_POST["sender_name"], FILTER_SANITIZE_STRING); //sender name 
    // $reply_to_email = filter_var($_POST["sender_email"], FILTER_SANITIZE_STRING); //sender email used in "reply-to" header 
    // $subject  = filter_var($_POST["subject"], FILTER_SANITIZE_STRING); //get subject from HTML form 
    //$message  = filter_var($_POST["message"], FILTER_SANITIZE_STRING); //message 

    /* //don't forget to validate empty fields 
    if(strlen($sender_name)<1) { 
     die('Name is too short or empty!'); 
    } 
    */ 

    //Get uploaded file data 
    $file_tmp_name = $_FILES['my_file']['tmp_name']; 
    $file_name  = $_FILES['my_file']['name']; 
    $file_size  = $_FILES['my_file']['size']; 
    $file_type  = $_FILES['my_file']['type']; 
    $file_error  = $_FILES['my_file']['error']; 

    if($file_error > 0) { 
     die('Upload error or No files uploaded'); 
    } 
    //read from the uploaded file & base64_encode content for the mail 
    $handle = fopen($file_tmp_name, "r"); 
    $content = fread($handle, $file_size); 
    fclose($handle); 
    $encoded_content = chunk_split(base64_encode($content)); 

    $boundary = md5("sanwebe"); 
    //header 
    $headers = "MIME-Version: 1.0\r\n"; 
    // $headers .= "From:".$from_email."\r\n"; 
    //$headers .= "Reply-To: ".$reply_to_email."" . "\r\n"; 
    $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; 

    //plain text 
    $body = "--$boundary\r\n"; 
    $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n"; 
    $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
    // $body .= chunk_split(base64_encode($message)); 

    //attachment 
    $body .= "--$boundary\r\n"; 
    $body .="Content-Type: $file_type; name=".$file_name."\r\n"; 
    $body .="Content-Disposition: attachment; filename=".$file_name."\r\n"; 
    $body .="Content-Transfer-Encoding: base64\r\n"; 
    $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; 
    $body .= $encoded_content; 

    $sentMail = @mail($recipient_email, $subject, $body, $headers); 
    if($sentMail) { 
     //output success or failure messages 
     die('Thank you for your email'); 
    } else { 
     die('Could not send mail! Please check your PHP mail configuration.'); 
    } 
} 
?> 
+0

** WARNING **: '' @オペレータを持つメソッドを呼び出すときにエラーを抑制しないでください。何かが間違っていたら、それについて知りたいし、是正措置を講じるか、ユーザーに役立つメッセージを表示するか、問題を記録するか、それ以上のことをする必要があります。また、深刻な問題を指摘しようとしているエラーを無視すると、このようなデバッグの問題がさらに複雑になります。 – tadman

+0

ファイルをアップロードした後、同じページにエラーメッセージを表示します。 –

答えて

0

あなたはこのコードを試すことができます

if(isset($_FILES['pic'])) 
    { 
     $target_dir = "./uploads/"; // upload directory 
     $pics = $_FILES['pic']; 

     //print_r($pics); 
     $target_file = $target_dir.$time.basename($pics["name"]); // destination path 
     $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION); // get file extension 
     // check uploaded file exists or not 
     if(file_exists($pics["tmp_name"])) 
     { 
      $check = getimagesize($pics["tmp_name"]); // get the image size 
      if(!$check) 
      { 
       $Error['pic'] = "File is not an image."; 
       $picErr="File is not an image."; 
      } 
      else if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") 
      { 
       $Error['pic'] = "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
       $picErr="Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
      } 
      else if(file_exists($target_file)) 
      { 
       $Error['pic'] = "Profile pic already exists"; 
       $picErr="Profile pic already exists"; 
      } 
      if(empty($Error['pic'])) 
      { 
       if(!move_uploaded_file($pics["tmp_name"], $target_file)) 
       { 
        $Error['pic'] = "Profile pic not uploaded"; 
        $picErr="Profile pic not uploaded"; 
       } 
       else 
       { 
        $pic = $time.$pics['name']; 
       } 
      } 
     } // file exists if end 
     else 
     { 
      $Error['pic'] = "Please select a image"; 
      $picErr="Please select a image"; 
     } 
    } 
関連する問題