2012-10-23 4 views
1

를 전자 메일로 보내기 나는 다음과 같은 HTML5 양식을 가지고 : 나는에 폴더를 만든파일 업로드 및 내 웹 사이트에 계정

<!doctype html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Site Name</title> 
<meta http-equiv="refresh" content="15;URL=http://mysiteaddress.com/"> 
</head> 

<style> 
    body { 
     background: #202024; 
     font: .75em Arial, Helvetica, sans-serif; 
     color: #FFF; 
     text-align: center; 
     margin-top: 25%; 
    } 
</style> 
<body> 

<?php 
    if(isset($_POST['email'])) { 

     // TO AND FROM 
     $email_to = "[email protected]"; 
     $email_subject = "Message from MYSITE.COM"; 

     function died($error) { 
      // ERROR MESSAGES TO THE USER 
      echo "We are very sorry, but there were error(s) found with the form you submitted. "; 
      echo "These errors appear below.<br /><br />"; 
      echo $error."<br /><br />"; 
      echo "Please go back and fix these errors.<br /><br />"; 
      die(); 
     } 

     // VALIDATION ON EXPECTED DATA 
     if(!isset($_POST['name']) || 
      !isset($_POST['email']) || 
      !isset($_POST['busname']) || 
      !isset($_POST['busdisc']) || 
      !isset($_POST['budget']) || 
      !isset($_POST['timeframe']) || 
      !isset($_POST['protitle']) || 
      !isset($_POST['prodisc']) || 
      !isset($_POST['target']) || 
      !isset($_POST['requirements'])) { 
      died('We are sorry, but there appears to be a problem with the form you submitted.');  
     } 

     $name_from = $_POST['name']; // required 
     $email_from = $_POST['email']; // required 
     $busname = $_POST['busname']; // not required 
     $busdisc = $_POST['busdisc']; // not required 
     $budget = $_POST['budget']; // not required 
     $timeframe = $_POST['timeframe']; // not required 
     $protitle = $_POST['protitle']; // not required 
     $prodisc = $_POST['prodisc']; // required 
     $uploads = $_POST['uploads']; // not required 
     $target = $_POST['target']; // not required 
     $requirements = $_POST['requirements']; // not required   

     // MANDATORY FIELDS 
     $error_message = ""; 
     $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
      if(!preg_match($email_exp,$email_from)) { 
      $error_message .= 'The email address you entered does not appear to be valid.<br />'; 
      } 
      $string_exp = "/^[A-Za-z .'-]+$/"; 
      if(!preg_match($string_exp,$name_from)) { 
      $error_message .= 'The name you entered does not appear to be valid.<br />'; 
      } 
      if(!preg_match($string_exp,$prodisc)) { 
      $error_message .= 'The project description you entered does not appear to be valid.<br />'; 
      } 

      if(strlen($error_message) > 0) { 
      died($error_message); 
      } 
     $email_message = "Services Form.\n\n"; 

     function clean_string($string) { 
      $bad = array("content-type","bcc:","to:","cc:","href"); 
      return str_replace($bad,"",$string); 
     } 

     $email_message .= "Name: ".clean_string($name_from)."\n"; 
     $email_message .= "Email: ".clean_string($email_from)."\n"; 
     $email_message .= "Business Name: ".clean_string($busname)."\n"; 
     $email_message .= "Business Description: ".clean_string($busdisc)."\n"; 
     $email_message .= "Budget: ".clean_string($budget)."\n"; 
     $email_message .= "Timeframe: ".clean_string($timeframe)."\n"; 
     $email_message .= "Project Title: ".clean_string($protitle)."\n"; 
     $email_message .= "Project Description: ".clean_string($prodisc)."\n"; 
     $email_message .= "Uploads: ".clean_string($uploads)."\n"; 
     $email_message .= "Target Audience: ".clean_string($target)."\n"; 
     $email_message .= "Further Requirements: ".clean_string($requirements)."\n"; 

     // FILE UPLOADS 
     $allowedExts = array("ai", "doc", "docx", "gif", "jpeg", "jpg", "pdf", "png", "psd"); 
     $extension = end(explode(".", $_FILES["uploads"]["name"])); 

     if ((($_FILES["file"]["type"] == "image/gif") 
     || ($_FILES["file"]["type"] == "image/jpeg") 
     || ($_FILES["file"]["type"] == "image/png") 
     || ($_FILES["file"]["type"] == "image/pjpeg")) 
     && ($_FILES["file"]["size"] < 20000) 
     && in_array($extension, $allowedExts)) { 
      if ($_FILES["file"]["error"] > 0) { 
      echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; 
      } 
      else { 
       echo "Upload: " . $_FILES["file"]["name"] . "<br />"; 
       echo "Type: " . $_FILES["file"]["type"] . "<br />"; 
       echo "Size: " . ($_FILES["file"]["size"]/1024) . " Kb<br />"; 
       echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; 
       if (file_exists("upload/" . $_FILES["file"]["name"])) { 
        echo $_FILES["file"]["name"] . " already exists. "; 
      } 
       else { 
       move_uploaded_file($_FILES["file"]["tmp_name"], 
       "upload/" . $_FILES["file"]["name"]); 
       echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; 
       } 
      } 
     } 
     else { 
      echo "Invalid file"; 
     } 

    // EMAIL HEADERS 
    $headers = 'From: '.$email_from."\r\n". 
    'Reply-To: '.$email_from."\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 
    @mail($email_to, $email_subject, $email_message, $headers); 
?> 

<!-- SUCCESS MESSAGE --> 
Your message has been sent. Thank you for contacting me, I'll get back to you as soon as possible. 

<?php 
} 
?> 
</body> 
</html> 

이 PHP 스크립트에 의해 처리되는

<form action="briefform.php" method="post" enctype="multipart/form-data" name="servicesform" id="servicesform" autocomplete="on"> 
    <fieldset> 
     <ul> 
      <li><label>Name*</label> 
       <input name="name" type="text" class="name"> 
      </li> 
      <li><label>Email*</label> 
       <input name="email" type="email" class="email"> 
      </li> 
      <li><label>Business Name</label> 
       <input name="busname" type="text" id="busname"> 
      </li> 
      <li><label>Business Description</label> 
       <textarea name="busdisc" id="busdisc"></textarea> 
      </li> 
      <li><label>Budget (AUD)</label> 
       <input name="budget" type="number" id="budget" placeholder="$"> 
      </li> 
      <li><label>Time Frame</label> 
       <input name="timeframe" type="text" id="timeframe"> 
      </li> 
      <li><label>Project Title</label> 
       <input name="protitle" type="text" id="protitle" > 
      </li> 
      <li><label>Project Description*</label> 
       <textarea name="prodisc" id="prodisc" spellcheck="true"></textarea> 
      </li> 
      <li><label>Upload</label> 
       <input name="uploads[]" type="file" id="uploads" multiple> 
      </li> 
      <li><label>Target Audience</label> 
       <textarea name="target" id="target"></textarea> 
      </li> 
      <li><label>Further Details</label> 
       <textarea name="requirements" id="requirements" spellcheck="true"></textarea> 
      </li> 
      <li><input type="reset" name="reset" class="reset" value="Reset"/> 
       <input type="submit" name="submit" class="submit" value="Send"/> 
      </li> 
     </ul> 
    </fieldset> 
</form> 

내 디렉토리 업로드, 내가 믿는 스크립트는 파일을 업로드하는 곳입니다.

사용자가 필수 입력란에 양식을 제출하면 정보가 내 이메일 계정으로 전송됩니다. 사용자뿐만 아니라 파일/파일을 전송하려는 경우

그러나, 스크립트가이 처리되지 않고 사용자가 "잘못된 파일을"수신 에코은 "귀하의 메시지 이상을 보냈습니다.. .. "피드백.

그때 내 이메일 계정으로 향할 경우에, 나는 성공적으로하지만, 사용자가 업로드 한 파일 (심지어 "잘못된 파일"오류와 함께) 내 에있는 누락되지 않은 전송 된 정보를 볼 수 있습니다 폴더를 업로드하십시오.

위의 스크립트를 사용하면 파일 업로드 부분을 어떻게 얻을 수 있습니까?

감사합니다.

+1

메시지가 아닙니다. – Peon

+0

@DainisAbols : 권자가 아니지만 모국어가 모르는 사람에게 이해하기가 어렵습니다. 롭, 그는 문법 오류를 지적합니다 : "당신은 메시지입니다 ..."가 아니라 "당신의 메시지 ..." – arkascha

+0

가능한 중복 : [w3schools PHP 보안 이메일 스크립트] (http://stackoverflow.com/ 질문/12541600/w3schools-php-secure-email-script) – hakre

답변

2

OK. 완전히 새로운 답변입니다. 코드에는 몇 가지 문제가 있습니다. 내 서버에서 실행하고 있습니다.

저는 지금 다중 파일 업로드를 시작하지 않을 것입니다.

<form action="briefform.php" method="post" enctype="multipart/form-data" name="servicesform" id="servicesform" autocomplete="on"> 
    <fieldset> 
     <ul> 
      <li><label>Name*</label> 
       <input name="name" type="text" class="name"> 
      </li> 
      <li><label>Email*</label> 
       <input name="email" type="email" class="email"> 
      </li> 
      <li><label>Business Name</label> 
       <input name="busname" type="text" id="busname"> 
      </li> 
      <li><label>Business Description</label> 
       <textarea name="busdisc" id="busdisc"></textarea> 
      </li> 
      <li><label>Budget (AUD)</label> 
       <input name="budget" type="number" id="budget" placeholder="$"> 
      </li> 
      <li><label>Time Frame</label> 
       <input name="timeframe" type="text" id="timeframe"> 
      </li> 
      <li><label>Project Title</label> 
       <input name="protitle" type="text" id="protitle" > 
      </li> 
      <li><label>Project Description*</label> 
       <textarea name="prodisc" id="prodisc" spellcheck="true"></textarea> 
      </li> 
      <li><label>Upload</label> 
       <input name="uploads" type="file" id="uploads" multiple> 
      </li> 
      <li><label>Target Audience</label> 
       <textarea name="target" id="target"></textarea> 
      </li> 
      <li><label>Further Details</label> 
       <textarea name="requirements" id="requirements" spellcheck="true"></textarea> 
      </li> 
      <li><input type="reset" name="reset" class="reset" value="Reset"/> 
       <input type="submit" name="submit" class="submit" value="Send"/> 
      </li> 
     </ul> 
    </fieldset> 
</form> 

그래서, 우선, 그것은되지 $ _POST [ '업로드']입니다 ..

<!doctype html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Site Name</title> 
<meta http-equiv="refresh" content="15;URL=http://mysiteaddress.com/"> 
</head> 

<style> 
    body { 
     background: #202024; 
     font: .75em Arial, Helvetica, sans-serif; 
     color: #FFF; 
     text-align: center; 
     margin-top: 25%; 
    } 
</style> 
<body> 

<?php 
    if(isset($_POST['email'])) { 

     // TO AND FROM 
     $email_to = "[email protected]"; 
     $email_subject = "Message from MYSITE.COM"; 

     function died($error) { 
      // ERROR MESSAGES TO THE USER 
      echo "We are very sorry, but there were error(s) found with the form you submitted. "; 
      echo "These errors appear below.<br /><br />"; 
      echo $error."<br /><br />"; 
      echo "Please go back and fix these errors.<br /><br />"; 
      die(); 
     } 

     // VALIDATION ON EXPECTED DATA 
     if(!isset($_POST['name']) || 
      !isset($_POST['email']) || 
      !isset($_POST['busname']) || 
      !isset($_POST['busdisc']) || 
      !isset($_POST['budget']) || 
      !isset($_POST['timeframe']) || 
      !isset($_POST['protitle']) || 
      !isset($_POST['prodisc']) || 
      !isset($_POST['target']) || 
      !isset($_POST['requirements'])) { 
      died('We are sorry, but there appears to be a problem with the form you submitted.');  
     } 

     $name_from = $_POST['name']; // required 
     $email_from = $_POST['email']; // required 
     $busname = $_POST['busname']; // not required 
     $busdisc = $_POST['busdisc']; // not required 
     $budget = $_POST['budget']; // not required 
     $timeframe = $_POST['timeframe']; // not required 
     $protitle = $_POST['protitle']; // not required 
     $prodisc = $_POST['prodisc']; // required 
     $uploads = $_FILES['uploads']; // not required 

스크립트에 대한 지금

<input name="uploads" type="file" id="uploads" multiple> 

에 양식을 변경합니다. 그것은 $ _FILES입니다. 스크립트에서 $ uploads 값을 사용해야합니다.

 $target = $_POST['target']; // not required 
     $requirements = $_POST['requirements']; // not required   

     // MANDATORY FIELDS 
     $error_message = ""; 
     $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
      if(!preg_match($email_exp,$email_from)) { 
      $error_message .= 'The email address you entered does not appear to be valid.<br />'; 
      } 
      $string_exp = "/^[A-Za-z .'-]+$/"; 
      if(!preg_match($string_exp,$name_from)) { 
      $error_message .= 'The name you entered does not appear to be valid.<br />'; 
      } 
      if(!preg_match($string_exp,$prodisc)) { 
      $error_message .= 'The project description you entered does not appear to be valid.<br />'; 
      } 

      if(strlen($error_message) > 0) { 
      died($error_message); 
      } 
     $email_message = "Services Form.\n\n"; 

     function clean_string($string) { 
      $bad = array("content-type","bcc:","to:","cc:","href"); 
      return str_replace($bad,"",$string); 
     } 

     $email_message .= "Name: ".clean_string($name_from)."\n"; 
     $email_message .= "Email: ".clean_string($email_from)."\n"; 
     $email_message .= "Business Name: ".clean_string($busname)."\n"; 
     $email_message .= "Business Description: ".clean_string($busdisc)."\n"; 
     $email_message .= "Budget: ".clean_string($budget)."\n"; 
     $email_message .= "Timeframe: ".clean_string($timeframe)."\n"; 
     $email_message .= "Project Title: ".clean_string($protitle)."\n"; 
     $email_message .= "Project Description: ".clean_string($prodisc)."\n"; 
     $email_message .= "Uploads: ".clean_string($uploads)."\n"; 
     $email_message .= "Target Audience: ".clean_string($target)."\n"; 
     $email_message .= "Further Requirements: ".clean_string($requirements)."\n"; 

     // FILE UPLOADS 
     $allowedExts = array("ai", "doc", "docx", "gif", "jpeg", "jpg", "pdf", "png", "psd"); 
     $extension = end(explode(".", $_FILES["uploads"]["name"])); 

여기까지 좋습니다. 아무데도 $ _FILES [ "file"]을 사용하기 시작 했으므로 튜토리얼을 함께 사용했다고 가정합니다. 어느 $ _FILES [ "업로드", 또는 $ 업로드이 시점

 if ((($_FILES["uploads"]["type"] == "image/gif") 
     || ($_FILES["uploads"]["type"] == "image/jpeg") 
     || ($_FILES["uploads"]["type"] == "image/png") 
     || ($_FILES["uploads"]["type"] == "image/pjpeg")) 
     && ($_FILES["uploads"]["size"] < 20000) 

이 정말 작은 이미지에 유효합니다. 20000 바이트는 20kB에 불과합니다. 이 부분을 여기에서 끌어 낸 것 같아 http://www.w3schools.com/php/php_file_upload.asp. 당신은 여기에 또 다른 문제입니다 2MB의 이미지

 && in_array($extension, $allowedExts)) { 

에 대한

 ($_FILES["uploads"]["size"] < 200000) 
200KB 이미지의

, 또는

 ($_FILES["uploads"]["size"] < 200000) 

를 사용할 수 있습니다. 더 쉽게 읽을 수 있도록 if 문을 확장합니다.메타 데이터는 GIF, PNG 또는 JPG입니다, 그리고 그것은 20킬로바이트 이하이며이 중 하나 어도비 일러스트 레이터, Word 문서, 이미지, PDF, 또는 포토샵 파일 인 경우

if(

    (
      ($_FILES["file"]["type"] == "image/gif" ) || 
      ($_FILES["file"]["type"] == "image/jpeg") || 
      ($_FILES["file"]["type"] == "image/png" ) || 
      ($_FILES["file"]["type"] == "image/pjpeg")  

    ) 

    && 

      ($_FILES["file"]["size"] < 20000) 

    && 

      in_array($extension, $allowedExts) 

) 

당신은 기본적으로 말한

이미지라면 사실입니다. AI, PDF 또는 DOC는 업로드 유형 검사를 통과하지 않습니다.

  if ($_FILES["uploads"]["error"] > 0) { 
      echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; 
      } 
      else { 
       echo "Upload: " . $_FILES["uploads"]["name"] . "<br />"; 
       echo "Type: " . $_FILES["uploads"]["type"] . "<br />"; 
       echo "Size: " . ($_FILES["uploads"]["size"]/1024) . " Kb<br />"; 
       echo "Temp file: " . $_FILES["uploads"]["tmp_name"] . "<br />"; 
       if (file_exists("upload/" . $_FILES["uploads"]["name"])) { 
        echo $_FILES["uploads"]["name"] . " already exists. "; 
      } 
       else { 
       move_uploaded_file($_FILES["uploads"]["tmp_name"], 
       "upload/" . $_FILES["uploads"]["name"]); 
       echo "Stored in: " . "upload/" . $_FILES["uploads"]["name"]; 
       } 
      } 
     } 
     else { 
      echo "Invalid file"; 
     } 

    // EMAIL HEADERS 
    $headers = 'From: '.$email_from."\r\n". 
    'Reply-To: '.$email_from."\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 
    @mail($email_to, $email_subject, $email_message, $headers); 
?> 

<!-- SUCCESS MESSAGE --> 
Your message has been sent. Thank you for contacting me, I'll get back to you as soon as possible. 

<?php 
} 
?> 
</body> 
</html> 
+0

아니요, 실제로 처리하려고 할 때 foreach 루프를 벗어났습니다. 그리고 그 코드는 키를 사용하지 않습니다. . – evandentremont

+0

먼저 하나의 이미지 만 사용하도록하십시오. – evandentremont

+1

코드를 업데이트하고 문제가있는 위치를 설명했습니다. 나는 3 일 동안 6 시간 동안 잠을 잔다. 이전 답변은 매우 철저하지 않았다. – evandentremont

관련 문제