Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. public function saveFileToTmpDir($fileId)
  2. {
  3. $baseTmpPath = $this->getBaseTmpPath();
  4. $uploader = $this->uploaderFactory->create(['fileId' => $fileId]);
  5. $uploader->setAllowedExtensions($this->getAllowedExtensions());
  6. $uploader->setAllowRenameFiles(true);
  7. $result = $uploader->save($this->varDirectory->getAbsolutePath($baseTmpPath));
  8.  
  9. if (isset($result['name'])) {
  10.  
  11. $fileNameComplete = $result['name'];
  12. $name = explode("_", $fileNameComplete);
  13.  
  14. $prefix = preg_match('/product-import/',$name[0]);
  15. $fileName = preg_match('/[0-9]/',$name[1]);
  16. $counter = preg_match('/[0-9]/',$name[2]);
  17.  
  18. if ($prefix == 1 && $fileName == 1 && $counter == 1 ) {
  19.  
  20. $result['tmp_name'] = str_replace('\\', '/', $result['tmp_name']);
  21. $result['path'] = str_replace('\\', '/', $result['path']);
  22. $result['url'] = $this->storeManager
  23. ->getStore()
  24. ->getBaseUrl(
  25. \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
  26. ) . $this->getFilePath($baseTmpPath, $result['file']);
  27. $result['name'] = $result['file'];
  28. if (isset($result['file'])) {
  29. try {
  30. $relativePath = rtrim($baseTmpPath, '/') . '/' . ltrim($result['file'], '/');
  31. $this->coreFileStorageDatabase->saveFile($relativePath);
  32. } catch (\Exception $e) {
  33. $this->logger->critical($e);
  34. throw new \Magento\Framework\Exception\LocalizedException(
  35. __('Something went wrong while saving the file(s).')
  36. );
  37. }
  38. }
  39. } else{
  40. return false;
  41. }
  42. }
  43. return true;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement