Home  >  Article  >  Backend Development  >  Zend Framework upload file rename

Zend Framework upload file rename

WBOY
WBOYOriginal
2016-07-25 09:11:32960browse
  1. //Instantial file special class
  2. $fName=$this->_request->getPost('fName');
  3. $adapter = new Zend_File_Transfer_Adapter_Http();
  4. //Folder to store uploaded files
  5. $adapter->setDestination('/opt/lampp/htdocs/blog/upload');
  6. //Upload configuration
  7. $adapter
  8. ->addValidator ( 'Extension', false, $configs['extension']) //File format restrictions
  9. ->addValidator('Size', false, array('min' =>floatval($configs['minsize']),
  10. 'max' => floatval($configs['maxsize '])))//Set the size of the uploaded file between 1-2M
  11. ->addValidator ( 'Count', false, array('min' => intval($configs['mincount']),
  12. 'max' => intval($configs['maxcount'])) );//Number of uploaded files
  13. //Rename configuration
  14. $fileInfo = $adapter->getFileInfo();//Get basic configuration
  15. $ extName=$this->getExtension($fileInfo);//Get the extension
  16. $filename=md5(time()+$fileInfo['fFile']['name']).'.'.$extName;/ /Rename
  17. $adapter->addFilter('Rename', array('target' => $filename, 'overwrite' => true)); //Perform rename
  18. //Return to appear after uploading Message
  19. if (!$adapter->receive())
  20. {
  21. $messages = $adapter->getMessages ();//Detection
  22. //Zend_Debug::dump($messages);
  23. $message='' ;
  24. if(is_array($messages))
  25. {
  26. foreach($messages as $k=>$v)
  27. {
  28. $message.=$k.':'.$v.'
    ';
  29. }
  30. }
  31. else
  32. {
  33. $message=$messages;
  34. }
  35. }
  36. else
  37. {
  38. $this->view->message='Upload successful! ';
  39. }
Copy code
  1. /**
  2. * Get file extension
  3. * @param String $name file noun
  4. * @author
  5. */
  6. public function getExtension ($name)
  7. {
  8. $fname='';
  9. if($name)
  10. {
  11. foreach ($name as $val)
  12. {
  13. $ fname=$val['name'];
  14. }
  15. $exts = @split("[/\.]", $fname) ;
  16. $n = count($exts)-1;
  17. $exts = $exts[ $n];
  18. return $exts;
  19. }
  20. }
Copy code


Statement:
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