PHP форму отправляем на почту с вложенным файлом

У меня есть скрипт для отправки формы PHP на почту с вложенным файлом, но у него не было функции для проверки размера и расширения файла.

код php ниже

### Attachment Preparation ###

$file_attached = false;
if(isset($_FILES['file_attach'])) //check uploaded file
{
//get file details we need
$file_tmp_name    = $_FILES['file_attach']['tmp_name'];
$file_name        = $_FILES['file_attach']['name'];
$file_size        = $_FILES['file_attach']['size'];
$file_type        = $_FILES['file_attach']['type'];
$file_error       = $_FILES['file_attach']['error'];

//exit script and output error if we encounter any
if($file_error>0)
{
$mymsg = array(
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
6=>"Missing a temporary folder" );

$output = json_encode(array('type'=>'error', 'text' => $mymsg[$file_error]));
die($output);
}

//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));
//now we know we have the file for attachment, set $file_attached to true
$file_attached = true;
}

if($file_attached) //continue if we have the file
{
# Mail headers should work with most clients
$headers = "MIME-Version: 1.0\r\n";
$headers = "X-Mailer: PHP/" . phpversion()."\r\n";
$headers .= "From: ".$from_email."\r\n";
$headers .= "Subject: ".$subject."\r\n";
$headers .= "Reply-To: ".$user_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary1')."\r\n\r\n";

$headers .= "--".md5('boundary1')."\r\n";
$headers .= "Content-Type: multipart/alternative;  boundary=".md5('boundary2')."\r\n\r\n";

$headers .= "--".md5('boundary2')."\r\n";
$headers .= "Content-Type: text/plain; charset=utf-8\r\n\r\n";
$headers .= $message_body."\r\n\r\n";

и это код формы php

<label>
<span>Attachment</span>
<input type="file" name="file_attach" class="input-field" />
</label>
<label>
<span>&nbsp;</span><input type="submit" id="submit_btn" value="Submit" />
</label>

Я не хочу использовать jQuery, я хочу сделать это с php.

1

Решение

Вы будете использовать что-то вроде этого для проверки расширения файла:

if($_FILES["file_attach"]["type"] == "image/jpeg") { //...

Замените «изображение / JPEG» с любым $_FILE['file_attach']['type'] значение для фактического типа файла, который вы хотите иметь.

Что касается проверки размера, вы будете делать что-то вроде:

if($_FILE["file_attach"]["size"] > 1000000) { //...

В конце концов, это, вероятно, лучший способ включить его в существующий скрипт:

//[...]$file_error       = $_FILES['file_attach']['error'];if($_FILES["file"]["type"] != "image/jpeg"){
$file_error = 7;
}elseif($_FILES["file_attach"]["size"] > 1000000) { // = 1MB
$file_error = 8;
}//exit script and output error if we encounter any
if($file_error>0)
{
$mymsg = array(
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
6=>"Missing a temporary folder"7=>"We do not accept files of that type.",
8=>"Files cannot be larger than 1MB" );

$output = json_encode(array('type'=>'error', 'text' => $mymsg[$file_error]));
die($output);
}
//[...]
1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector