问题描述:

I've made contact form with php, and validation for name, email and message works great, but my file size validation doesn't work. When I try to upload file larger then 2 MB it's loading and collapse.

I've run out of ideas what to do. Can anyone help me?

Here is my php code:

<?php

if ($_POST["submit"]) {

if (!$_POST['name']){

$error="Unesite svoje ime.";

}

if (!$_POST['email']) {

$error.="Unesite e-mail adresu.";

}

if (!$_POST['message']) {

$error.="Unesite tekst poruke";

}

if ($_POST['email']!="" AND !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) {

$error.="Unesite validu e-mail adresu.";

}

if(!empty($_FILES['my-file']['name'])){

$file_name = $_FILES['my-file']['name'];

$temp_name = $_FILES['my-file']['tmp_name'];

$file_type = $_FILES['my-file']['type'];

if($_FILES['my-file']['size'] > 524288){

$error.="Maksimalna velicina fajla je 5 MB.";

} else {

$from = $_POST['email'];

$to = "[email protected]";

$subject = "Poruka sa sajta";

$message = "Tekst poruke:".$_POST['message'];

$file = $temp_name;

$content = chunk_split(base64_encode(file_get_contents($file)));

$uid = md5(uniqid(time()));

$header = "MIME-Version: 1.0\r\n";

$header .= "From:".$from."\r\n";

$header .= "Reply-To: ".$_POST['email']."" . "\r\n";

$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

$header .= "This is a multi-part message in MIME format.\r\n";

//plain text part

$header .= "--".$uid."\r\n";

$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";

$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";

$header .= $message."\r\n\r\n";

//attachment part

$header .= "--".$uid."\r\n";

$header .= "Content-type: ".$file_type."; name=\"".$file_name."\"\r\n";

$header .= "Content-Transfer-Encoding: base64\r\n";

$header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";

$header .= $content."\r\n\r\n";

if ($error) {

$result='<div class="alert alert-danger">'.$error.'</div>';

} else {

if (mail($to, $subject, "", $header)) {

$result='<div class="alert alert-success"><strong>Vasa poruka je poslata!</strong></div>';}

else {

$result='<div class="alert alert-danger">Došlo je do greške prilikom slanja poruke. Molim Vas pokušajte ponovo kasnije.</div>';}

} //if there is no error

}} else {if ($error) {

$result='<div class="alert alert-danger">'.$error.'</div>';

} else {

$header = "From:".$_POST['email']."\r\n";

$header = "Reply-To: ".$_POST['email']. "\n" ;

$body = "Tekst poruke:".$_POST['message'];

if (mail('[email protected]', 'Poruka sa sajta', $body, $header)) {

$result='<div class="alert alert-success"><strong>Vasa poruka je poslata!</strong></div>';}

else {

$result='<div class="alert alert-danger">Došlo je do greške prilikom slanja poruke. Molim Vas pokušajte ponovo kasnije.</div>';}

}}//if files not empty

} //if is submited

?>

and my HTML code:

<div class="col-md-6 emailForm">

<?php echo $result; ?>

<form id="contact_body" method="post" action="index.php" enctype="multipart/form-data">

<div class="form-group">

<label for="name">Ime i prezime:</label>

<input type="text" name="name" class="form-control" placeholder="Ime i prezime" data-required="true"/>

</div>

<div class="form-group">

<label for="email">E-mail:</label>

<input type="text" name="email" class="form-control" placeholder="E-mail" data-required="true"/>

</div>

<div class="form-group">

<label for="comment">Poruka:</label>

<textarea class="form-control" name="message" data-required="true"></textarea>

</div>

<div class="form-group">

<label class="btn btn-primary">

<input type="file" name="my-file" style="display:none;" onchange="$('#upload-file-info').html(this.files[0].name);">

Dodajte sliku

</label>

<span class='label label-info' id="upload-file-info"></span>

</div>

<div id="push" class="pushbutton"></div>

<input type="submit" name="submit" class="btn btn-lg" value="Poslati"/>

</form>

</div>

网友答案:

You can get error - because you have set lower value for php upload setting:

; Maximum allowed size for uploaded files.

upload_max_filesize = 40M

; Must be greater than or equal to upload_max_filesize

post_max_size = 40M

If you reach this limits - the file is not uploaded and $_FILES[..][error]!=0.

And the second - 524288 is not 5MB but only 524kB

网友答案:

add a zero

if($_FILES['my-file']['size'] > 5242880)
相关阅读:
Top