Uploading File Attachments Using Php Form Mail
This article shows yous how to create a PHP based email form that supports file zipper. The article will likewise show you lot how to validate the type and size of the uploaded file.
Note: Information technology is like shooting fish in a barrel to create consummate file upload forms using Simfatic Forms. Visually design the form, become form submissions by e-mail, salvage submissions to database and more than. No coding required. Read more than near Simfatic Forms here.
The HTML form with file upload box
The code for an HTML class with a file upload box is given below. User can click on the 'Browse' push button to select the file from his/her local motorcar.
<form method = "POST" name = "email_form_with_php" action = "php-form-action.php" enctype = "multipart/form-data"> <label for = 'name'>Name: </characterization> <input type = "text" name = "name" > <label for = 'email'>Email: </label> <input type = "text" name = "email" > <label for = 'message'>Message:</label> <textarea name = "message"></textarea> <label for = 'uploaded_file'>Select A File To Upload:</label> <input blazon = "file" name = "uploaded_file"> <input blazon = "submit" value = "Submit" name = 'submit'> </course>
The form will look similar this:
Please note that we have added:
"enctype="multipart/form-data"
while defining the <form>
tag. This is to tell the browser that this form will exist used to upload files. And then nosotros accept added the "proper noun" and "email" fields to collect the user info. The third form field is the file upload box.
<input type = "file" name = "uploaded_file">
On hitting the "Submit" button, the form data forth with the file data is posted to the script pointed to by the 'activity' attribute of the form.
Getting the uploaded file in the PHP script
In the PHP script, we will start validate the submission and if the validation succeeds, we will send the submission past electronic mail.
We can access the uploaded file and its different attributes by using the $_FILES assortment. This array volition contain the name, size, path and other attributes of the uploaded file. The code below gets the proper noun, type, and size of the uploaded file:
//Go the uploaded file data $name_of_uploaded_file = basename($_FILES['uploaded_file']['proper noun']); //become the file extension of the file $type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1); $size_of_uploaded_file = $_FILES["uploaded_file"]["size"]/1024;//size in KBs
The code above is getting the different attributes of the uploaded file from the $_FILES[]
array.
Validating the size and extension of the uploaded file
Suppose we don't want to permit files greater than the size of 100KB and nosotros just desire to allow image files to be uploaded. The validation code goes like this:
//Settings $max_allowed_file_size = 100; // size in KB $allowed_extensions = array("jpg", "jpeg", "gif", "bmp"); //Validations if($size_of_uploaded_file > $max_allowed_file_size ) { $errors .= "\due north Size of file should exist less than $max_allowed_file_size"; } //------ Validate the file extension ----- $allowed_ext = false; for($i=0; $i<sizeof ( $ allowed_extensions ) ; $ i + + ) { if ( strcasecmp ( $ allowed_extensions [ $ i ] , $ type_of_uploaded_file ) = = 0 ) { $ allowed_ext = true; } } if ( ! $ allowed_ext ) { $ errors . = " \ northward The uploaded file is not supported file type . " . " Only the following file types are supported: " . implode ( ' , ' , $ allowed_extensions ) ; }
In the code higher up, we are validating the file size and type. We accept the maximum allowed file ($max_allowed_file_size) size set to 100KB. The $allowed_extensions array contains the file extensions of all immune file types. The validation lawmaking checks to see whether the file extension matches whatever of the extensions in the $allowed_extensions assortment.
If there are errors establish in the validation, the error is displayed. Else we proceed with sending the email.
Copy the uploaded file
Now, its fourth dimension to send the uploaded file with the user bulletin to the recipient'south email address.
Start of all, nosotros shall copy the file to a folder on the server.
//copy the temp. uploaded file to uploads folder $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file; $tmp_path = $_FILES["uploaded_file"]["tmp_name"]; if(is_uploaded_file($tmp_path)) { if(!re-create($tmp_path,$path_of_uploaded_file)) { $errors .= '\northward mistake while copying the uploaded file'; } }
This code copies the uploaded file to the 'uploads' folder. You tin change the uploads folder by updating $upload_folder. Please make sure that "uploads" folder has "777" permissions.
Sending the Email
The side by side stride is to compose and ship the email. We volition utilise the Pear library for composing and sending the e-mail. ( see the Pear installation instructions beneath ) The pear classes PEAR::Post and PEAR::Mail_Mime are used for sending the email with the attachment.
Showtime, we need to include the pear library files for these classes.
include_once('Mail.php'); include_once('Mail_Mime/mime.php');
The code below composes and sends the electronic mail
$bulletin = new Mail_mime(); $message->setTXTBody($text); $message->addAttachment($path_of_uploaded_file); $body = $message->become(); $extraheaders = assortment("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email); $headers = $bulletin->headers($extraheaders); $mail service = Postal service::factory("post"); $postal service->send($to, $headers, $trunk);
Mail_mime()
class helps in composing a MIME message. In the lawmaking above, a Mail_mime object is created, the text body is updated ( $bulletin->setTXTBody($text);
) and the attachment is added ( $message->addAttachment(file)
)
The MIME-encoded message is then sent using the Mail grade.
The sample PHP upload course
Click here to download php-email-form-attachment.cypher
The download contains a consummate PHP upload form that sends the uploaded by email.
How to Install the PEAR Library
In this commodity, we used the PEAR::Postal service and PEAR::Mail_Mime classes to send the email with attachment. Before using these classes, you lot demand to install the PEAR package on your server. It is beyond the scope of this tutorial to hash out the installation of PEAR. But, I want to give you lot a quick tip. Get the PEAR installer script from
http://pear.php.internet/get-pear
Save the file as "pear-installer.php". Upload this file to your server in any directory. Then run this file from your browser, like this:
http://www.yourdomain.com/pear-installer.php
This brandish the web interface to install the PEAR on your website. The interface shows detailed instructions. After Pear is installed, search and install packages "mail" and "mail_mime".
Besides see: more than free file upload forms from ReusableForms.com
See Likewise
- A Simple E-mail Form Straight Out of the Oven
- How to Add an Email Form in Your WordPress Website
- Sending form submissions to email using 'mailto:'
- Why is my PHP script not sending emails?
Source: https://html.form.guide/email-form/php-email-form-attachment/
0 Response to "Uploading File Attachments Using Php Form Mail"
Enviar um comentário