Пример #1
0
 public function uploadFile($form_field_name)
 {
     include_once Yii::app()->basePath . '/extensions/FileUploader.php';
     $file = new FileUploader();
     $com_id = Yii::app()->user->com_id;
     if (!empty($_FILES[$form_field_name]['name'])) {
         $file->setRootDir(dirname(dirname(Yii::app()->basePath)) . '/img');
         //$file->setExt(array('txt', 'doc', 'docx', 'xls', 'xlsx', 'zip', 'rar', 'pdf','ppt','pptx','cvs','gif','png','jpg','jpeg','png','dwg'));   //txt|doc|docx|xls|ppt|xlsx|pptx|zip|rar
         $file->setExt(array('txt', 'doc', 'docx', 'xls', 'xlsx', 'zip', 'rar', 'pdf', 'gif', 'png', 'jpg', 'jpeg', 'png'));
         $file->setFormField($form_field_name);
         $file->setMaxSize('50M');
         $file_ext = strtolower(pathinfo($_FILES[$form_field_name]['name'], PATHINFO_EXTENSION));
         $file_name = md5(sha1_file($_FILES[$form_field_name]['tmp_name'])) . '.' . $file_ext;
         $dir = 'upload/price_check/' . $com_id . '/' . date('Y') . '/' . date('m') . '/' . date('d') . '/';
         if ($f = $file->save($file_name, $dir)) {
             //成功处理,$f是保存后的文件的相对路径
             $data['file_name'] = $_FILES[$form_field_name]['name'];
             $data['file_path'] = $f;
             return $data;
         } else {
             $this->ajax_echo($file->getErrorCode(), $file->getErrorMsg());
         }
     }
 }