Advertisement
Guest User

Untitled

a guest
Apr 25th, 2014
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. $adapter = new ZendFileTransferAdapterHttp();
  2.  
  3. $adapter->addFilter('filerenameupload', array(
  4. 'target' => BASE_DIR . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR .
  5. 'img' . DIRECTORY_SEPARATOR . 'gallery' .
  6. DIRECTORY_SEPARATOR . 'image.jpg',
  7. 'randomize' => true,
  8. ));
  9.  
  10. var_dump($adapter->isValid()); // true
  11.  
  12. if (!$adapter->receive()) {
  13. $messages = $adapter->getMessages();
  14. echo implode("n", $messages);
  15. }
  16.  
  17. protected function moveUploadedFile($sourceFile, $targetFile)
  18. {
  19. ErrorHandler::start();
  20. $result = move_uploaded_file($sourceFile, $targetFile);
  21. $warningException = ErrorHandler::stop();
  22. if (!$result || null !== $warningException) {
  23. throw new ExceptionRuntimeException(
  24. sprintf("File '%s' could not be renamed. An error occurred while processing the file.", $sourceFile), 0, $warningException
  25. );
  26. }
  27.  
  28. return $result;
  29. }
  30.  
  31. public function uploadAction()
  32. {
  33. $adapter = new ZendFileTransferAdapterHttp();
  34.  
  35. $adapter->setDestination(BASE_DIR . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR .
  36. 'img' . DIRECTORY_SEPARATOR . 'gallery' . DIRECTORY_SEPARATOR);
  37. // Returns all known internal file information
  38.  
  39. $adapter->addFilter('FileRename', array('target' => $adapter->getDestination() .
  40. DIRECTORY_SEPARATOR . rand(2, 10) . '.jpeg',
  41. 'overwrite' => true));
  42.  
  43. if (!$adapter->receive()) {
  44. $messages = $adapter->getMessages();
  45. return new ViewModel(['messages' => $messages]);
  46. } else {
  47. $this->flashMessenger()->addSuccessMessage('Upload success');
  48.  
  49. $this->redirect()->toRoute('admin', ['controller' => 'gallery',
  50. 'action' => 'index']);
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement