/** * Event listener function of the article backend module. * Uploads ESD-File */ public function uploadEsdFileAction() { $destinationDir = Shopware()->DocPath('files_' . Shopware()->Config()->get('sESDKEY')); try { $fileBag = new \Symfony\Component\HttpFoundation\FileBag($_FILES); /** @var $file Symfony\Component\HttpFoundation\File\UploadedFile */ $file = $fileBag->get('fileId'); $file->move($destinationDir, $file->getClientOriginalName()); } catch (Exception $e) { $this->View()->assign(array('success' => false, 'message' => $e->getMessage())); return; } if ($file === null) { $this->View()->assign(array('success' => false)); return; } $this->View()->assign(array('success' => true)); }
/** * The uploadAction function is responsible for the uploading of media. * If no album id passed, the uploaded media is assigned to the unsorted album. * * @return bool */ public function uploadAction() { $params = $this->Request()->getParams(); //try to get the transferred file try { $file = $_FILES['fileId']; if($file['size'] < 1 && $file['error'] === 1 || empty($_FILES)){ throw new Exception("The file exceeds the max file size."); } $fileInfo = pathinfo($file['name']); $fileExtension = strtolower($fileInfo['extension']); $file['name'] = $fileInfo['filename'].".".$fileExtension; $_FILES['fileId']['name'] = $file['name']; $fileBag = new \Symfony\Component\HttpFoundation\FileBag($_FILES); /**@var $file UploadedFile*/ $file = $fileBag->get('fileId'); } catch (Exception $e) { die(json_encode(array('success' => false, 'message' => $e->getMessage()))); } if ($file === null) { die(json_encode(array('success' => false))); } $fileInfo = pathinfo($file->getClientOriginalName()); $extension = $fileInfo['extension']; if (in_array(strtolower($extension), $this->blackList)) { unlink($file->getPathname()); unlink($file); die(json_encode(array('success' => false, 'blacklist' => true, 'extension' => $extension))); } //create a new model and set the properties $media = new Media(); $params['attribute'] = $params['attribute'][0]; //set media album, if no one passed set the unsorted album. if (isset($params['albumID'])) { $media->setAlbumId($params['albumID']); $media->setAlbum(Shopware()->Models()->find('Shopware\Models\Media\Album', $params['albumID'])); } else { $media->setAlbumId(-10); $media->setAlbum(Shopware()->Models()->find('Shopware\Models\Media\Album', -10)); } $media->setDescription(''); $media->setCreated(new DateTime()); $identity = Shopware()->Auth()->getIdentity(); if ($identity !== null) { $media->setUserId($identity->id); } else { $media->setUserId(0); } //set the upload file into the model. The model saves the file to the directory $media->setFile($file); try { //persist the model into the model manager Shopware()->Models()->persist($media); Shopware()->Models()->flush(); $data = $this->getMedia($media->getId())->getQuery()->getArrayResult(); $this->Response()->setHeader('Content-Type', 'text/plain'); die(json_encode(array('success' => true, 'data' => $data[0]))); } catch (\Doctrine\ORM\ORMException $e) { die(json_encode(array('success' => false, 'message' => $e->getMessage()))); } }