April 25, 2009

Send mail with attachment in php

This code will help you with attachment of files in anytype in sending a mail with php

include this input field inside "form" tag
<input type="file" name="fileatt" size="22" />
<input type="submit" name="career_submit" size="22" />

When the page is submitted use the following code to send the mail to the receipent with the attachment.

if(isset($_REQUEST['career_submit'])) {
/*@@@@@@@@@@@--- File Type --- @@@@@@@@@@@*/
$fileatt_name = $_FILES['fileatt']['name'];
$fileName = $_FILES['fileatt']['tmp_name'];
$fileatt_type = "application/octet-stream";

$email_from = $_REQUEST["email"]; // Who the email is from
$email_subject = "Mail Subject"; // The Subject of the email
$email_to = "example@example.com"; // Who the email is too

$headers = "From: ".$email_from;

/*@@@@@@@--- File Permissions --- @@@@@@@@*/
$file = fopen($fileName,'rw');
@$data = fread($file,filesize($fileName));

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

$email_message .= 'Your Message.....'; // Mail Content Comes Here

$email_message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";

$data = chunk_split(base64_encode($data));

$email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n";

$ok = @mail($email_to, $email_subject, $email_message, $headers);

April 24, 2009

Allow only selected filetype in attachment using javascript

This code in javascript checks the attachment type,

You can add this code in javascript validation on form submit event to upload only specific file types,

if(document.formname.fieldname.value != "") {
MyFile = document.formname.fieldname.value /* replace with the correct value */
FileArray = MyFile.split("\\")
FileName = FileArray[FileArray.length-1]
ExtArray = FileName.split(".")
Ext = ExtArray[ExtArray.length-1]
Ext = Ext.toUpperCase(Ext)

if(!(Ext=="DOCX" || Ext=="DOC" || Ext=="TXT" || Ext=="PDF" || Ext=="RTF"))
alert("Invalid Upload File! Upload doc or txt or pdf or rtf file only")
return false

Explanation of the code:

The selected file for attachment is assigned to the variable "MyFile". With the build-in javascript split() the filename and the file extensions are splitted. To allow browser compatible converting the file extension to uppercase using "toUpperCase" function. When the if condition satisfied "return false" to terminate the form submission.

April 23, 2009

Max length for Mysql database TEXT types

MySQL supports 4 TEXT field types (TINYTEXT, TEXT, MEDIUMTEXT and LONGTEXT) and this post looks at the maximum length of each of these field types.

MyISAM tables in MySQL have a maximum size of a row of 65,535 bytes, so all the data in a row must fit within that limit. However, the TEXT types are stored outside the table itself and only contribute 9 to 12 bytes towards that limit. (For more information about this refer to the MySQL Manual - Data Storage Requirements chapter).

TEXT data types are also able to store much more data than VARCHAR and CHAR text types so TEXT types are what you need to use when storing web page or similar content in a database.

The maximum amount of data that can be stored in each data type is as follows:
TINYTEXT 256 bytes
TEXT 65,535 bytes ~64kb
MEDIUMTEXT 16,777,215 bytes ~16MB
LONGTEXT 4,294,967,295 bytes ~4GB

In most circumstances the TEXT type is probably sufficient, but if you are coding a content management system it's probably best to use the MEDIUMTEXT type for longer pages to ensure there are no issues with data size limits.

April 22, 2009

Enter only numbers in a text field using javascript

We can able to restrict entering special characters inside the text input field in a form

Call "forceNumber(event)" function the "onKeyPress" event on input field like this,

<input name="text" maxlength="17" onkeypress="return forceNumber(event);" />

Use the following function to check in javascript itself

<script language="javascript">
function forceNumber(event){
var keyCode = event.keyCode ? event.keyCode : event.charCode;
if((keyCode < 48 || keyCode > 58) && keyCode != 8 && keyCode != 9 && keyCode != 32 && keyCode != 37 && keyCode != 39 && keyCode != 40 && keyCode != 41 && keyCode != 43 && keyCode != 45 && keyCode != 46)
return false;


This function will allow numbers from 0-9 and characters "+ ()-"
and it will be more useful in telephone number validation inside a form

Note: the function "forceNumber()" should be inside <head>.