Written  and Posted by ePatronus Enterprise IT Solutions Islamabad, Pakistan.

view:

<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'careerform',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
'htmlOptions'=>array(
'enctype'=>'multipart/form-data',
),
)); ?>
 <div>
<div>
 <h2 style="margin: 15px 0 0 0;">careers Form</h2>
 <div>
 <div>
 <?php echo $form->labelEx($model,'Name'); ?>
 <?php echo $form->textField($model,'name',array('class'=>'cu-txt')); ?>
 <?php echo $form->error($model,'name',array('class'=>'errormessage')); ?>
 </div>
<div>
 <?php echo $form->labelEx($model,'E-mail'); ?>
 <?php echo $form->textField($model,'email',array('class'=>'cu-txt')); ?>
 <?php echo $form->error($model,'email',array('class'=>'errormessage')); ?>
 </div>
 <div>
 <?php echo $form->labelEx($model,'Contact No.'); ?>
 <?php echo $form->textField($model,'contact',array('class'=>'cu-txt')); ?>
 <?php echo $form->error($model,'contact',array('class'=>'errormessage')); ?>
 </div>
<div>
 <?php echo $form->labelEx($model,'position'); ?>
 <?php echo $form->dropDownList($model,'position',$model->positionlist(),
 array ( 'prompt' => '---Select position---' ));?>
 <?php echo $form->error($model,'position',array('class'=>'errormessage')); ?>
</div>
<div>
 <?php echo $form->labelEx($model,'Attach CV'); ?>
 <?php echo CHtml::activeFileField($model,'filename',array('id'=>'uploaded_file'));?>
 </div>
 <?php echo CHtml::linkButton('Submit'); ?>
 <?PHP echo CHtml::hiddenField('filehide',0, array ('id'=>'filehide')) ?>
<?PHP if(isset($_POST['filehide'])) { ?>
<?PHP } ?>
<?php $this->endWidget(); ?>

————————————————————————————————————————-

model:

<?php
/**
 * Careers class.
 * Careers is the data structure for keeping
 * Careers form data. It is used by the 'Careers' action of 'SiteController'.
 */
 class Careers extends CFormModel
 {
 public $name;
 public $email;
 public $contact;
 public $file;
 public $position;
 public $body;
 public $verifyCode;
 public $filename;
 public $path;
/**
 * Declares the validation rules.
 */
 public function rules()
 {
 return array(
 // name, email, subject and body are required
 array('name, email, contact, file, position', 'required'),
array('position', 'length', 'max'=>50),
array('filename', 'length', 'max'=>200),
array('path', 'length', 'max'=>300),
// email has to be a valid email address
 array('email', 'email'),
 // verifyCode needs to be entered correctly
);
 }
/**
 * Declares customized attribute labels.
 * If not declared here, an attribute would have a label that is
 * the same as its name with the first letter in upper case.
 */
 public function attributeLabels()
{
return array(
 'name' => 'Name',
 'email' => 'Email',
 'contact'=>'contact',
 'position' => 'Position',
 'filename' => 'Filename',
 'path' => 'Path',
 );
}
public function positionlist()
 {
 return array('Web Developer'=>'Web Developer',
 'Web Designer'=>'Web Designer',
 'Network Engineer'=>'Network Engineer',
 'HRM'=>'HRM',
 'Internee'=>'Internee',
 );
 }
 }

————————————————————————————————————————-

controller:

public function actionCareers()
 {
$model=new Careers;
if(isset($_POST['filehide']))
 {
 $model->attributes=$_POST['Careers'];
 $file = CUploadedFile::getInstance($model,'filename');
 $model->filename = $file->name;
 $model->path = 'tester/';
$file->saveAs($model->path.$model->filename,true);
 $subject ="companyname.com<CV Received>";
 $name =  $model->name;
 $email = $model->email;
 //$website = $Clientinfo->website;
 $contact =$model->contact;
 $position = $model->position;
// Allowed file types. add file extensions WITHOUT the dot.
 $allowtypes=array("zip", "rar", "doc","docx", "pdf", "txt", "png", "jpg", "gif");
// Require a file to be attached: false = Do not allow attachments true = allow only 1 file to be attached
 $requirefile="true";
// Maximum file size for attachments in KB NOT Bytes for simplicity. MAKE SURE your php.ini can handel it,
 // post_max_size, upload_max_filesize, file_uploads, max_execution_time!
 // 2048kb = 2MB,       1024kb = 1MB,     512kb = 1/2MB etc..
 $max_file_size="1024";
// Thank you message
 $thanksmessage="Your email has been sent, we will respond shortly.";
//checks attachment file
 // checks that we have a file
 if((!empty($_FILES["Careers"]["filename"])) && ($_FILES['Careers']['error']['filename'] == 0)) {
 // basename -- Returns filename component of path
 $filename = basename($_FILES['Careers']['name']['filename']);
 $ext = substr($filename, strrpos($filename, '.') + 1);
 $filesize=$_FILES['Careers']['size']['filename'];
 $max_bytes=$max_file_size*1024;
//Check if the file type uploaded is a valid file type.
 if (!in_array($ext, $allowtypes)) {
 $errors[]="Invalid extension for your file: <strong>".$filename."</strong>";
// check the size of each file
 } elseif($filesize > $max_bytes) {
 $errors[]= "Your file: <strong>".$filename."</strong> is to big. Max file size is ".$max_file_size."kb.";
 }
} // if !empty FILES
if (empty($errors)) { //If everything is OK
$fileatt      = $model->path.$model->filename;
 $fileatt_type = $_FILES['Careers']['type']['filename'];
 $fileatt_name = $_FILES['Careers']['name']['filename'];
// Headers
 $headers = "From: $email";
// create a boundary string. It must be unique
 $semi_rand = md5(time());
 $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
 $headers .= "\nMIME-Version: 1.0\n" .
 "Content-Type: multipart/mixed;\n" .
 " boundary=\"{$mime_boundary}\"";
// Add a multipart boundary above the plain message
 $message ="This is a multi-part message in MIME format.\n\n";
 $message.="--{$mime_boundary}\n";
 $message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
 $message.="Content-Transfer-Encoding: 7bit\n\n";
 $message.="From: ".$email."\n";
 $message.="Contact: ".$contact."\n";
 $message.="Position: ".$position."\n\n";
 //echo is_uploaded_file($fileatt);
 //print_r($_FILES);
if ($fileatt_name!="") {
 // Read the file to be attached ('rb' = read binary)
 $file = fopen($fileatt,'rb');
 $data = fread($file,filesize($fileatt));
 fclose($file);
// Base64 encode the file data
 $data = chunk_split(base64_encode($data));
// Add file attachment to the message
 $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";
 }
mail(Yii::app()->params['adminEmail'], $subject, $message, $headers);
}
}
 $this->render('careers', array('model'=>$model));
}

————————————————————————————————————————-

we are waiting for your feedback!!

Leave a Reply

Your email address will not be published. Required fields are marked *