Yii File Tutorials

CHtml FileField

Using CHtml class we are creating fileField in yii framework. This post will help you to understand about how to upload file in different method, how to get and save the uploaded file in controller, file validation in model yii framework.SyntaxactiveFileField With CHtmlfileField With FormfileField With CHtml (Without Model Name)File In ControllerDONT FORGET multipart/form-data <?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'files-form', 'htmlOptions'=>array('enctype' => 'multipart/form-data'), )); ?> Syntax public static string activeFileField(CModel $model, string […]... Read More »

Yii Zip Format

In this article, I explained about “How to handle zip files” in yii framework. I used this code to my project for handling the zip files. Here i added source code to create, Extract, Download and Delete zip files in yii framework.Create Zip Files in YiiExtract Zip Files in YiiDownload Zip Files in YiiDelete Zip Files in YiiCreate Zip Files in Yii <?php public function actionCreateZip(){ $zip=new ZipArchive(); $destination=DIRDetails."/filename.zip"; if($zip->open($destination,ZIPARCHIVE::CREATE) […]... Read More »

CSV File To DB In Yii

CSV File UploadRead CSV In ControllerCSV File UploadUsing the below code i created the form “importcsvform.php”.importcsvform<div class="form"> <?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'csv-form', 'enableAjaxValidation'=>false, 'htmlOptions'=>array('enctype' => 'multipart/form-data'), )); ?> <?php //echo $form->errorSummary($model); ?> <div class="row"> <?php echo $form->labelEx($model,'csvfile'); ?> <?php $this->widget('CMultiFileUpload', array( 'model'=>$model, 'name' => 'csvfile', 'max'=>1, 'accept' => 'csv', 'duplicate' => 'Duplicate file!', 'denied' => 'Invalid file type', )); ?> <?php echo $form->error($model,'csvfile'); ?> </div> <div class="row buttons"> <?php echo CHtml::submitButton('Import',array("id"=>"Import",'name'=>'Import')); […]... Read More »

Yii CMultiFileupload

PropertiesCMultifile In ViewCMultifile In controllerModel ValidationCMultiFileUpload widget functionMaximum File Size Validation On Before UploadDONT FORGET multipart/form-data <?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'files-form', 'htmlOptions'=>array('enctype' => 'multipart/form-data'), ));?> PropertiesCMultiFileUpload generates a file input that can allow uploading multiple files at a time. It have the following properties.accept - Allowed file types duplicate - String message file - File name htmlOptions - additional html options id - CMultiFileUpload widget id max - max number of […]... Read More »

Yii 1.0 Delete Files

This post will help you to learn “how to delete files in yii framework”. I deleted singele file and all files from directory. Delete All Files Inside DirectoryDelete Single FileDelete Zip FilesDelete All Files Inside Directoryphp glob() method used to read all files from directory of yii application and unlink() method used to delete the files. //your directory $dir=Yii::app()->basePath.'/files/foldername/'; foreach(glob($dir.'*.*') as $files) { unlink($files); } When we use above method […]... Read More »

Delete zip file in yii

Delete Zip Files Using PHPHere i added this code to delete the zip files inside the folder of yii framework.SourceCode: public function actionDeletefile($id,$docid) { $error=''; $filepath=Yii::app()->basePath.'/files/'.$zipfilename.".zip"; $zip = new ZipArchive; if ($zip->open($filepath) === TRUE) { $zip->deleteName($filename); // this file of inside zip folder } else{ $error='failed'; } $zip->close(); }... Read More »

Yii 1.0 Download Zip files

Download Zip Files Using YiiHere I have added the code to download the Zip files in yii folder.SourceCode: public function downloadfinal($docid,$docname) { $filename=Dirdetails"filename.zip"; if(file_exists($filename)){ $path_parts = pathinfo($filename); $ext = strtolower($path_parts["extension"]); switch ($ext) { case "pdf": $ctype="application/pdf"; break; case "exe": $ctype="application/octet-stream"; break; case "zip": $ctype="application/zip"; break; case "doc": $ctype="application/msword"; break; case "xls": $ctype="application/vnd.ms-excel"; break; case "ppt": $ctype="application/vnd.ms-powerpoint"; break; case "gif": $ctype="image/gif"; break; case "png": $ctype="image/png"; break; case "jpg": $ctype="image/jpg"; break; default: […]... Read More »

Yii 1.0 Create Zip file

Create Zip Files Using PHPI added the source code to create a zip file in yii. public function actionCreatefile($filename,$filelist) { $zip=new ZipArchive(); $destination=DIRDetails."/filename.zip"; if($zip->open($destination,ZIPARCHIVE::CREATE) !== true) { return false; } foreach($filelist as $thefile) { $random=rand(11111,99999); $filename=$random.$thefile; $zip->addFile($thefile->tempname,$filename); } $zip->close(); }... Read More »

Yii 1.0 Extract Zip files

Extract Zip Files Using YiiI have added the source code to extract the zip files using yii public function actionExtractfile($filename) { $filename='filename'; $zipfile=DirDetails."/filename.zip"; $zip = zip_open($zipfile); $extract=DirDetails."/newfolder"; if($zip) { if(!is_dir($extract)) mkdir($extract); while ($zip_entry = zip_read($zip)) { // if(zip_entry_name($zip_entry)==$filename) //if you need any specified file use this condition { $fp = fopen($extract."/".zip_entry_name($zip_entry), "w"); if (zip_entry_open($zip, $zip_entry, "r")) { $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); fwrite($fp,"$buf"); zip_entry_close($zip_entry); fclose($fp); break; } } } zip_close($zip); } […]... Read More »

Yii 1.0 Update Zip files

Update Zip Files Using PHPI added the source code to add new file into existing zip files. public function actionUpdatezip($zipfilename,$filelist) { $destination=Yii::app()->basePath.'/files/'.$zipfilename.".zip"; $zip=new ZipArchive(); if(!$zip->open($destination)) { return false; } if($filelist) { foreach($filelist as $thefile) { $filemodel=new Filesmodle; $randno=rand(11111,99999); $filename=$randno.$thefile->name; // yii magic method $zip->addFile($thefile->tempname,$filename); //$fileext=$thefile->extensionName; //$filemodel->Size=$thefile->size; } } $zip->close(); }... Read More »