php - How to echo success message on same page and only upload image type file -
this code running well.i want trying solve upload image type png jpeg. , if error or success message shown on same page. , 1 thing mail going in spam box . please regarding .
<form enctype="multipart/form-data" method="post" action=""> <label>your profile pic<input type="file" name="my_file" /></label> <label><input type="submit" name="button" value="submit" /></label> </form> <?php if($_post && isset($_files['my_file'])) { $from_email = 'sanjay@.com'; //from mail, mandatory hosts $recipient_email = 'sanjay@.com'; //recipient email (most cases personal email) //capture post data html form , sanitize them, //$sender_name = filter_var($_post["sender_name"], filter_sanitize_string); //sender name // $reply_to_email = filter_var($_post["sender_email"], filter_sanitize_string); //sender email used in "reply-to" header // $subject = filter_var($_post["subject"], filter_sanitize_string); //get subject html form //$message = filter_var($_post["message"], filter_sanitize_string); //message /* //don't forget validate empty fields if(strlen($sender_name)<1) { die('name short or empty!'); } */ //get uploaded file data $file_tmp_name = $_files['my_file']['tmp_name']; $file_name = $_files['my_file']['name']; $file_size = $_files['my_file']['size']; $file_type = $_files['my_file']['type']; $file_error = $_files['my_file']['error']; if($file_error > 0) { die('upload error or no files uploaded'); } //read uploaded file & base64_encode content mail $handle = fopen($file_tmp_name, "r"); $content = fread($handle, $file_size); fclose($handle); $encoded_content = chunk_split(base64_encode($content)); $boundary = md5("sanwebe"); //header $headers = "mime-version: 1.0\r\n"; // $headers .= "from:".$from_email."\r\n"; //$headers .= "reply-to: ".$reply_to_email."" . "\r\n"; $headers .= "content-type: multipart/mixed; boundary = $boundary\r\n\r\n"; //plain text $body = "--$boundary\r\n"; $body .= "content-type: text/plain; charset=iso-8859-1\r\n"; $body .= "content-transfer-encoding: base64\r\n\r\n"; // $body .= chunk_split(base64_encode($message)); //attachment $body .= "--$boundary\r\n"; $body .="content-type: $file_type; name=".$file_name."\r\n"; $body .="content-disposition: attachment; filename=".$file_name."\r\n"; $body .="content-transfer-encoding: base64\r\n"; $body .="x-attachment-id: ".rand(1000,99999)."\r\n\r\n"; $body .= $encoded_content; $sentmail = @mail($recipient_email, $subject, $body, $headers); if($sentmail) { //output success or failure messages die('thank email'); } else { die('could not send mail! please check php mail configuration.'); } } ?>
you can try code
if(isset($_files['pic'])) { $target_dir = "./uploads/"; // upload directory $pics = $_files['pic']; //print_r($pics); $target_file = $target_dir.$time.basename($pics["name"]); // destination path $imagefiletype = pathinfo($target_file, pathinfo_extension); // file extension // check uploaded file exists or not if(file_exists($pics["tmp_name"])) { $check = getimagesize($pics["tmp_name"]); // image size if(!$check) { $error['pic'] = "file not image."; $picerr="file not image."; } else if($imagefiletype != "jpg" && $imagefiletype != "png" && $imagefiletype != "jpeg" && $imagefiletype != "gif") { $error['pic'] = "sorry, jpg, jpeg, png & gif files allowed."; $picerr="sorry, jpg, jpeg, png & gif files allowed."; } else if(file_exists($target_file)) { $error['pic'] = "profile pic exists"; $picerr="profile pic exists"; } if(empty($error['pic'])) { if(!move_uploaded_file($pics["tmp_name"], $target_file)) { $error['pic'] = "profile pic not uploaded"; $picerr="profile pic not uploaded"; } else { $pic = $time.$pics['name']; } } } // file exists if end else { $error['pic'] = "please select image"; $picerr="please select image"; } }
Comments
Post a Comment