ZendFramework上传文件重命名
时间:2021-07-01 10:21:17
帮助过:18人阅读
<无详细内容>
- //实例化文件上专类
- $fName=$this->_request->getPost('fName');
- $adapter = new Zend_File_Transfer_Adapter_Http();
- //存放上传文件的文件夹
- $adapter->setDestination('/opt/lampp/htdocs/blog/upload');
- //上传配置
- $adapter
- ->addValidator ( 'Extension', false, $configs['extension'])//文件格式限制
- ->addValidator('Size', false, array('min' =>floatval($configs['minsize']),
- 'max' => floatval($configs['maxsize'])))//设置上传文件的大小在1-2M之间
- ->addValidator ( 'Count', false, array('min' => intval($configs['mincount']),
- 'max' => intval($configs['maxcount'])) );//上传文件数量
- //重命名配置
- $fileInfo = $adapter->getFileInfo();//获取基本配置
- $extName=$this->getExtension($fileInfo);//获取扩展名
- $filename=md5(time()+$fileInfo['fFile']['name']).'.'.$extName;//重命名
- $adapter->addFilter('Rename', array('target' => $filename, 'overwrite' => true));//执行重命名
- //返回上传后出现在信息
- if (!$adapter->receive())
- {
- $messages = $adapter->getMessages ();//检测
- //Zend_Debug::dump($messages);
- $message='';
- if(is_array($messages))
- {
- foreach($messages as $k=>$v)
- {
- $message.=$k.':'.$v.'
'; - }
- }
- else
- {
- $message=$messages;
- }
- }
- else
- {
- $this->view->message='上传成功!';
- }
- /**
- * 获取文件扩展名
- * @param String $name 文件名词
- * @author
- */
- public function getExtension ($name)
- {
- $fname='';
- if($name)
- {
- foreach ($name as $val)
- {
- $fname=$val['name'];
- }
- $exts = @split("[/\\.]", $fname) ;
- $n = count($exts)-1;
- $exts = $exts[$n];
- return $exts;
- }
- }
|