Zend Framework上傳檔案重新命名

來源:互聯網
上載者:User
<無詳細內容>
  1. //執行個體化檔案上專類
  2. $fName=$this->_request->getPost('fName');
  3. $adapter = new Zend_File_Transfer_Adapter_Http();
  4. //存放上傳檔案的檔案夾
  5. $adapter->setDestination('/opt/lampp/htdocs/blog/upload');
  6. //上傳配置
  7. $adapter
  8. ->addValidator ( 'Extension', false, $configs['extension'])//檔案格式限制
  9. ->addValidator('Size', false, array('min' =>floatval($configs['minsize']),
  10. 'max' => floatval($configs['maxsize'])))//設定上傳檔案的大小在1-2M之間
  11. ->addValidator ( 'Count', false, array('min' => intval($configs['mincount']),
  12. 'max' => intval($configs['maxcount'])) );//上傳檔案數量
  13. //重新命名配置
  14. $fileInfo = $adapter->getFileInfo();//擷取基本配置
  15. $extName=$this->getExtension($fileInfo);//擷取副檔名
  16. $filename=md5(time()+$fileInfo['fFile']['name']).'.'.$extName;//重新命名
  17. $adapter->addFilter('Rename', array('target' => $filename, 'overwrite' => true));//執行重新命名
  18. //返回上傳後出現在資訊
  19. if (!$adapter->receive())
  20. {
  21. $messages = $adapter->getMessages ();//檢測
  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='上傳成功!';
  39. }
複製代碼
  1. /**
  2. * 擷取副檔名
  3. * @param String $name 檔案名稱詞
  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. }
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.