Find us on facebook

Jul 17, 2014

Uploading image/file by creating a directory on the server in yii

^^^^ Form must have 'htmlOptions' => array(
        'enctype' => 'multipart/form-data',
    ),
^^^^^ model array('image', 'file', 'types'=>'jpg, gif, png','allowEmpty'=>true,'on'=>'update'),
^^^^^ Random name
$rnd = rand(0,9999);
$uploadedFile=CUploadedFile::getInstance($model,'image');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name

            $model->image = $fileName;
$model->attributes=$_POST['Vehicle'];
$model->tax_document=CUploadedFile::getInstance($model,'tax_document');
$model->mot_document=CUploadedFile::getInstance($model,'mot_document');
$model->image=CUploadedFile::getInstance($model,'image');
$model->save();
if($model->save())

  $path="document/vechicle/";

if(!file_exists('document/vechicle/'.$model->id)){
mkdir('document/vechicle/'.$model->id, 0777,true);
chmod('document/vechicle/'.$model->id, 0777);
}
if ($model->tax_document){
$model->tax_document->saveAs('document/vechicle/'.$model->id.'/taxt'.$model->tax_document);
}
if ($model->mot_document){
$model->mot_document->saveAs('document/vechicle/'.$model->id.'/mot'.$model->mot_document);
}
if ($model->image){
$model->image->saveAs('document/vechicle/'.$model->id.'/img'.$model->image);

$this->redirect(array('view','id'=>$model->id));

No comments:

Post a Comment