Sunday, September 7, 2014

Upload Multiple files using Yii CUploadedFile class



Following code help your to upload multiple files using Yii CUploadedFile Class.

Model settings:

In your model class add rules for file tag  validation. Then add a attribute to your file tag and name it as "files"

example:

class Gallery extends CActiveRecord
{
public $files;
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'gallery';
}

public function rules()
{
return array(
array('files', 'file', 'types'=>'jpg, gif, png'),
array('files,asset_id','safe','on'=>"editrights")
   
    );
}
}

In your Controller:

Create a object for your class model. Then render your upload view file with param of gallery object.

example:

// place this code inside your action
$model=new Gallery;

if(isset($_POST["Gallery"])) {

// form massive assignment
$model->attributes = $_POST["Gallery"];
// uploader dir
$dir = "uploads/gallery/";
// get the array of CUploadedFile objects per files //

$fileobjects = CUploadedFile::getInstances($model,'files');
/*
* without model object you place following code
* $filemodel = CUploadedFile::getInstances('Gallery[files]');
*/
foreach( $fileobjects as $file) {
    // generate file name
    $randno=rand(11111,99999);
    $filename=$randno.$file->name;
    $dirfile = $dir.$filename;
    // save file in your location ensure your files dir is writable //
    $file->saveAs($dirfile);
    // save your file name
    $save = new Gallery;
    $save->name = $filename;
    $save->type = $file->extensionName;
    $save->save(false);
    chmod($dirfile, 0777);
    // if you want resize the image user following code //
    //
    if(preg_match('/image/',$file->type)) {
        // use convert command for resize file before make sure you get installed imagegick tool //
        echo exec("convert ".$file." -scale 220 ".$dir."thumb_".$filename);   
    }
}
$this->render("upload",array("model",$model));

In your view page:

Add file code in your form.

echo CHtml::activeFileField($model,'files[]',array("id"=>"files","multiple"=>true,"style"=>"margin-top:5px;"));


For more CUploadedFile options see CUploadedFile class reference


Thursday, August 28, 2014

Model Object to result array in Yii


Yii CActiverecord methods findall, findAllByattributes always return object. if you want get the result as array. By you can get the dbcriteria use it in commanBuilder

$object = Message::model();
$object = $object ->getCommandBuilder()
               ->createFindCommand($object ->tableSchema, $object ->dbCriteria)
               ->queryAll();

print_r($object);


Monday, August 25, 2014

Steps to install vagrant on windows system


This will help you to work with Virtual machines.. and helps us to work with Git


A) VirtalBox Dwonlad and install

B) Vagrant Download and install

Restart the system

Goto command line prompt

Create the Folder in your system using mkdir

1.. Start the vagrant
vagrant init

This command will create the config files

2.. Coping the vagrant from server. this is installing the Ubuntu 

vagrant box add precise32   http://files.vagrantup.com/precise32.box

3.. Now change the following line in the config file
box to prcecise32 in vagrant config

4.. Now the time to boot the new Ubuntu

vagrant up

5. This command will helps to stop the ubuntu

vagrant suspend

6. This command will helps to reboot the ubuntu
vagrant reload
 
 

Thursday, June 26, 2014

Print Triangle using Php

<?php

function triangle($t=10) {

for($j=(10-$t)/2;$j>0;$j--){ echo " "; }
for($i=$t;$i>=0;$i--){
echo "*";
}
echo "\n";
if($t>1)triangle($t-2);
}
triangle();
?>

Bind Some Event to Element and trigger that function when click the element

Scenario: Suppose if we used same click event function in various web pages. if you want do some logic some page button for that need to re...