yii文件下传

yii文件上传

页面 
------------------------------------------------- 

Html代码  yii文件下传
  1. <?php $form=$this->beginWidget('CActiveForm', array(  
  2.         'id'=>'add-form',  
  3.         'enableClientValidation'=>true,  
  4.         'clientOptions'=>array( 'validateOnSubmit'=>true,),  
  5.         'htmlOptions'=>array('enctype'=>'multipart/form-data'),    
  6.     ));   
  7. ?>  
  8.     <table>  
  9.         <tr>  
  10.             <td width="20%">  
  11.                 <b>ファイルパス:</b>&nbsp;&nbsp;<font color="red">*</font>  
  12.             </td>  
  13.             <td width="80%">  
  14.                 <?php echo CHtml::activeFileField($model, 'file'); ?>  
  15.                 <?php echo $form->error($model,'file');?>  
  16.             </td>  
  17.         </tr>  
  18.     </table>  
  19.   
  20.     <table>  
  21.         <tr>  
  22.             <td>  
  23.                 <?php echo CHtml::button('上传', array('submit' => array('downfiles/upload'))); ?>  
  24.             </td>  
  25.         </tr>  
  26.     </table>  




Model 
------------------------------------------------- 

Php代码  yii文件下传
  1. public function rules()  
  2. {  
  3.     return array(  
  4.                   
  5.         array('file''file','allowEmpty'=>true ,  
  6.                 'types'=>'jpg, gif, png, doc, txt',  
  7.                 'maxSize'=>1024 * 1024 * 10, // 10MB  
  8.                 'tooLarge'=>'The file was larger than 10MB. Please upload a smaller file.',  
  9.             ),  
  10.     );  
  11. }  



上传 
------------------------------------------------- 

Php代码  yii文件下传
  1. public function actionUpload(){  
  2.       
  3.     $model = new DownFiles();  
  4.       
  5.     if(isset($_POST["DownFiles"])){  
  6.           
  7.         $model->attributes=$_POST['DownFiles'];  
  8.   
  9.         $file = CUploadedFile :: getInstance($model'file');  
  10.           
  11.         if(is_null($file)){  
  12.             yii::app ()->user->setFlash('failed''请选择上传文件');  
  13.             $this->render('upload'array('model' => $model));  
  14.             return ;  
  15.         }  
  16.           
  17.         if (is_object($file) && get_class($file) == 'CUploadedFile') {  
  18.               
  19.             Yii::log("File Name : "  . $file->getName() );  
  20.               
  21.             // 文件类型  
  22.             $model->fileType = strtolower($file->getExtensionName());  
  23.   
  24.             // 存储文件名  
  25.             $newFileName = date('YmdHis') . '_' . rand(1000, 9999) . '.' . $model->fileType;   
  26.             // 服务器端存储路径  
  27.             $newFilepath = Yii::app()->params['upload_folder'] . $newFileName;  
  28.   
  29.             // 上传文件名  
  30.             $model->fileName = $file->getName();  
  31.             // 文件类型 (application/x-msdownload、application/pdf、application/octet-stream)  
  32.             $model->fileType = $file->getType();  
  33.             // 文件大小  
  34.             $model->fileSize = $file->getSize();  
  35.               
  36.             if ($model->validate()  && $model->save()){  
  37.                 // 将文件存在在服务器端  
  38.                 $file->saveAs($newFilepath);  
  39.   
  40.                 yii::app ()->user->setFlash('successed''上传成功');  
  41.             } else {  
  42.                 yii::app ()->user->setFlash('failed''上传失败');  
  43.             }  
  44.               
  45.         } else {  
  46.             yii::app ()->user->setFlash('failed''上传失败');  
  47.         }  
  48.           
  49.         $this->render('upload'array('model' => $model));  
  50.           
  51.     }else{  
  52.         $this->render('upload'array(  
  53.             'model' => $model,  
  54.         ));  
  55.     }  
  56.       
  57. }  






文件 
------------------------------------------------- 

Java代码  yii文件下传
  1. public function actionDownload(){  
  2.       
  3.     if (isset($_GET["id"])) {  
  4.         $id = $_GET["id"];  
  5.   
  6.         $model = DownFiles::model()->find('id =:id', array('id' => $id));  
  7.           
  8.         if ($model == null) {  
  9.             throw new CHttpException ('500''文件不存在');  
  10.         } else {  
  11.             // 服务器端文件的路径  
  12.             $fileName = $model->saveFilePath ;  
  13.               
  14.             if (file_exists($fileName)){  
  15.                 yii::app ()->request->sendFile ($model->fileName,  file_get_contents ($fileName));  
  16.             }  
  17.         }  
  18.     }  
  19. }