2011-10-26 2 views
2

안녕하세요. http://apptools.com에있는 코드를 찾았습니다. 사용자가 파일 업로드 세부 정보를 입력 할 수있게 해주는 양식을 만들었습니다. 그러면 자동으로 나에게 이메일을 보냅니다. 큰. 나는 당신이 내가 doc, pdf, psd, ai, jpg, bmp 그리고 어쩌면 더 많은 장래에 파일 형식을 어떻게 제한 할 수 있는지를 아는 지 궁금해했고 크기를 5MB로 제한했다. 내가 가지고있는 코드는 다음과 같습니다.파일 형식과 크기를 PHP 형식으로 업로드 제한

<?php 
if(!isset($_POST['Submit'])){ 
echo "error; you need to submit the form!"; 
} 
else { 


$to="[email protected]"; 
$subject="Subject header"; 
$enquiry = $_POST['message2']; 
$company = $_POST['company2']; 
$tel = $_POST['tel2']; 
$client = $_POST['fromname']; 
$clientemail = $_POST['fromemail']; 
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">"; 
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x"; 
$headers = "From: $from\r\n" . 
"MIME-Version: 1.0\r\n" . 
"Content-Type: multipart/mixed;\r\n" . 
" boundary=\"{$mime_boundary}\""; 

$message="Email from $fromname on Contact form.\n\n Details are as follows:\n Name: 
$client \n Company: $company \n Email: $clientemail \n Telephone: $tel \n Message: 
$enquiry \n". 
"\n Please contact them within 48 hours."; 

$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"; 
foreach($_FILES as $userfile){ 
$tmp_name = $userfile['tmp_name']; 
$type = $userfile['type']; 
$name = $userfile['name']; 
$size = $userfile['size']; 


if (file_exists($tmp_name)){ 


if(is_uploaded_file($tmp_name)){ 
$file = fopen($tmp_name,'rb'); 
$data = fread($file,filesize($tmp_name)); 
fclose($file); 
$data = chunk_split(base64_encode($data)); 
} 

$message .= "--{$mime_boundary}\n" . 
"Content-Type: {$type};\n" . 
" name=\"{$name}\"\n" . 
"Content-Disposition: attachment;\n" . 
" filename=\"{$fileatt_name}\"\n" . 
"Content-Transfer-Encoding: base64\n\n" . 
$data . "\n\n"; 
} 
} 
$message.="--{$mime_boundary}--\n"; 
if (@mail($to, $subject, $message, $headers)) 
echo "Message Sent"; 
else 
echo "Failed to send"; 
} ?> 

도움이 될 것입니다.

답변

2

당신은 php.ini 파일의 업로드 크기 (here)를 제한 할 수 있습니다, 그리고 당신이 그것을 허용 형식 (테스트하지 코드)입니다 확인하기 위해 업로드 된 파일의 이름의 확장자를 확인할 수 있습니다

$okExtensions = array('jpg', 'png'); 
$fileName = 'test.doc'; 

$fileParts = explode('.', $fileName); 

if(in_array(strtolower(end($fileParts)), $okExtensions)) 
{ 
    // proceed 
}