About the implementation method of renaming uploaded files in Zend Framework

不言
Release: 2023-04-01 09:46:01
Original
1862 people have browsed it

这篇文章主要介绍了Zend Framework上传文件重命名的实现方法,结合实例形式分析了Zend Framework上传文件重命名的具体操作步骤与配置、检测、重命名操作的相关实现技巧,需要的朋友可以参考下

本文实例讲述了Zend Framework上传文件重命名的实现方法。分享给大家供大家参考,具体如下:

1. Zend Framework文件上传重命名

//实例化文件上专类
$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.&#39;:&#39;.$v.&#39;<br>&#39;;
    }
  }
  else
  {
    $message=$messages;
  }
}
else
{
  $this->view->message=&#39;上传成功!&#39;;
}
Copy after login

2. 获取文件扩展名

/**
 * 获取文件扩展名
 * @param String $name 文件名词
 * @author
 */
public function getExtension ($name)
{
  $fname=&#39;&#39;;
  if($name)
  {
    foreach ($name as $val)
    {
      $fname=$val[&#39;name&#39;];
    }
    $exts = @split("[/\\.]", $fname) ;
    $n = count($exts)-1;
    $exts = $exts[$n];
    return $exts;
  }
}
Copy after login

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

关于Zend Framework动作控制器的用法

Zend Framework中的Application和Bootstrap的用法

Zend Framework动作助手的用法解析

The above is the detailed content of About the implementation method of renaming uploaded files in Zend Framework. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template