2014-08-04 3 views
0

다음 코드를 사용하면 사용자가 양식을 제출할 때 파일 (이미지/비디오)을 업로드해야하지만, 강제적 인 것은 아님이 이상적입니다.PHP 양식 업로드 및 전자 메일

<?php 

$to = "[email protected]"; 
$fromEmail = $_POST['fieldFormEmail']; 
$fromName = $_POST['fieldFormName']; 
$subject = $_POST['fieldSubject']; 
$message = $_POST['fieldDescription']; 

/* GET File Variables */ 
$tmpName = $_FILES['attachment']['tmp_name']; 
$fileType = $_FILES['attachment']['type']; 
$fileName = $_FILES['attachment']['name']; 

/* Start of headers */ 
$headers = "From: $fromName"; 

if (file($tmpName)) { 
    /* Reading file ('rb' = read binary) */ 
    $file = fopen($tmpName,'rb'); 
    $data = fread($file,filesize($tmpName)); 
    fclose($file); 

    /* a boundary string */ 
    $randomVal = md5(time()); 
    $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x"; 

    /* Header for File Attachment */ 
    $headers .= "\nMIME-Version: 1.0\n"; 
    $headers .= "Content-Type: multipart/mixed;\n" ; 
    $headers .= " boundary=\"{$mimeBoundary}\""; 

    /* Multipart Boundary above message */ 
    $message = "This is a multi-part message in MIME format.\n\n" . 
    "--{$mimeBoundary}\n" . 
    "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
    "Content-Transfer-Encoding: 7bit\n\n" . 
    $message . "\n\n"; 

    /* Encoding file data */ 
    $data = chunk_split(base64_encode($data)); 

    /* Adding attchment-file to message*/ 
    $message .= "--{$mimeBoundary}\n" . 
    "Content-Type: {$fileType};\n" . 
    " name=\"{$fileName}\"\n" . 
    "Content-Transfer-Encoding: base64\n\n" . 
    $data . "\n\n" . 
    "--{$mimeBoundary}--\n"; 
} 

$flgchk = mail ("$to", "$subject", "$message", "$headers"); 

if($flgchk){ 
    echo "A email has been sent to: $to"; 
} 
else{ 
    echo "Error in Email sending"; 
} 
?> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Email Attachment Without Upload - Excellent Web World</title> 
<style> 
body{ font-family:Arial, Helvetica, sans-serif; font-size:13px;} 
th{ background:#999999; text-align:right; vertical-align:top;} 
input{ width:181px;} 
</style> 
</head> 
<body> 
    <form action="Contact.php" method="post" name="mainform" enctype="multipart/form-data"> 
    <table width="500" border="0" cellpadding="5" cellspacing="5"> 
     <tr> 
     <th>Your Name</th> 
     <td><input name="fieldFormName" type="text"></td> 
    </tr> 
    <tr> 
    <tr> 
     <th>Your Email</th> 
     <td><input name="fieldFormEmail" type="text"></td> 
    </tr> 

    <tr> 
     <th>Subject</th> 
     <td><input name="fieldSubject" type="text" id="fieldSubject"></td> 
    </tr> 
    <tr> 
     <th>Comments</th> 
     <td><textarea name="fieldDescription" cols="20" rows="4" id="fieldDescription"></textarea></td> 
    </tr> 
    <tr> 
     <th>Attach Your File</th> 
     <td><input name="attachment" type="file"></td> 
    </tr> 
    <tr> 
     <td colspan="2" style="text-align:center;"><input type="submit" name="Submit" value="Send"><input type="reset" name="Reset" value="Reset"></td> 
    </tr> 
    </table> 
    </form> 
</body> 
<html> 

내가이 직면하고 문제,

는 페이지로드에서 실행
  1. , 파일이 업로드되지 않은 경우
  2. , 그것은 경고가 발생합니다.

"파일 이름이 존재하면"파일 업로드 부분을 넣으려고했지만 첨부 파일을 보내지 않았습니다. 도와주세요. 이 파일 이름은 경고를 발생, 파일이 업로드되지 않은 경우는

if(isset($_POST['Submit']) && ($_POST['Submit']) == 'Send') 
{ 
    /* process only when submit button whose name='Submit' 
    and value= 'Send' is pressed 
    entire PHP code */ 
} 

에서

묶으 메일 전송 기능 페이지로드에서 실행 Contact.php

답변

4

입니다 .

체크

if((empty($_POST['attachment'])) || (empty($_FILES['attachment']))){ 
    //file is not attached, show error 
}else{ 
//file is attached, process it and send via mail 
} 
PHP

에 접속하기 전에