public function addAction() { if (!$this->zfcUserAuthentication()->hasIdentity()) { return $this->redirect()->toRoute('zfcuser'); } $form = new UploadForm(); $optionSubject = $this->getSubjectTable()->getSubjectsForSelect(); $form->get('subject')->setAttribute('options', $optionSubject); $optionCategory = $this->getCategoryTable()->getCategoriesForSelect(); $form->get('category')->setAttribute('options', $optionCategory); $request = $this->getRequest(); if ($request->isPost()) { $file = new File(); $data = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray()); $form->setData($data); $uploadPath = $this->getOptions()->getUploadFolderPath(); // Validatoren $size = new Size(array('max' => $this->getOptions()->getMaxFileSizeInByte())); $extension = new Extension($this->getOptions()->getAllowedFileExtensions()); // Filter für Zufallsnamen if ($this->options->getRandomizeFileName()) { $rename = new Rename(array('target' => $uploadPath . '/file', 'randomize' => true)); } else { $rename = null; } //TODO Add to factory $adapter = new Http(); $adapter->setValidators(array($size, $extension)); $adapter->setFilters(array($rename)); if (!$adapter->isValid()) { $dataError = $adapter->getMessages(); array_merge($dataError, $adapter->getErrors()); foreach ($dataError as $key => $row) { echo $row; } header('HTTP/1.1 500 Internal Server Error'); exit; } else { $adapter->setDestination($uploadPath); if ($adapter->receive()) { $subjectID = $data['subject']; $categoryID = $data['category']; $dbdata = array(); $dbdata['fileName'] = $data['file']['name']; $filename = $adapter->getFileName(); if (is_array($filename)) { $dbdata['url'] = basename(current($filename)); } else { $dbdata['url'] = basename($filename); } $file->exchangeArray($dbdata); $this->getFileTable()->saveFile($file, $subjectID, $categoryID); header('HTTP/1.1 200 OK'); exit; } } } return array('form' => $form); }
public function importAction() { $request = $this->getRequest(); $this->form->get('type')->setValue($this->params('type')); if ($request->isPost()) { $nonFile = $request->getPost()->toArray(); $file = $this->params()->fromFiles('fileupload'); $data = array_merge($nonFile, ['fileupload' => $file['name']]); //set data post and file ... $this->form->setData($data); if ($this->form->isValid()) { //$size = new Size(array('max' => '204800B')); //minimum bytes filesize $adapter = new Http(); //$adapter->setValidators(array($size), $file['name']); if (!$adapter->isValid()) { $dataError = $adapter->getMessages(); $error = array(); foreach ($dataError as $key => $row) { $error[] = $row; } $this->form->setMessages(['fileupload' => $error]); } else { $pathUploadFiles = $this->importer->getDriverFactory()->getConfig()['file_upload_path']; if (!is_dir($pathUploadFiles)) { mkdir($pathUploadFiles, 0775, true); } $adapter->setDestination($pathUploadFiles); //\Zend\Debug\Debug::dump([$adapter->getFileName($file['name'])]); die(__METHOD__); /*\Zend\Debug\Debug::dump([ $file['name'], $adapter->receive($adapter->getFileName('fileupload', false)), $adapter->isFiltered($file['name']), $adapter->getMessages() ]); die(__METHOD__.__LINE__);*/ //$fileName = $adapter->getFileName('fileupload', false); if ($adapter->receive($adapter->getFileName('fileupload', false))) { $this->importer->import($this->params('type'), $adapter->getFileName('fileupload')); $this->prepareMessages(); } } } } //\Zend\Debug\Debug::dump([$form->getName(), get_class($form)]); die(__METHOD__); return ['form' => $this->form]; }
public function uploadImage($file) { $filename = $file['file']['name']; $adapter = new Http(); $path = BASE_PATH . '/img/position'; $fileRenameFilter = new Rename(array('target' => $path . '/usr.jpg', 'randomize' => true)); $adapter->addFilters(array($fileRenameFilter)); if ($adapter->receive($filename)) { $fileFullPath = explode('/', $adapter->getFileName()); $newFileName = array_pop($fileFullPath); return $newFileName; } else { return 'no-image.png'; } }
public function uploadImage($fileData) { $upload = new Http(); $upload->setDestination(self::UPLOAD_PATH); try { // upload received file(s) $upload->receive(); } catch (\Exception $e) { // return $uploadResult; } //This method will return the real file name of a transferred file. $name = $upload->getFileName($fileData['upload']['name']); //This method will return extension of the transferred file $extention = pathinfo($name, PATHINFO_EXTENSION); //get random new name $newName = $this->random->getRandomUniqueName(); $newFullName = self::UPLOAD_PATH . $newName . '.' . $extention; // rename rename($name, $newFullName); $uploadResult = $newFullName; return $uploadResult; }
private function uploadAttachment($filename, $attachmentPath) { $uploadResult = null; $upload = new Http(); $upload->setDestination($attachmentPath); try { // upload received file(s) $upload->receive($filename); } catch (\Exception $e) { return $uploadResult; } //This method will return the real file name of a transferred file. $name = $upload->getFileName($filename); //This method will return extension of the transferred file $extention = pathinfo($name, PATHINFO_EXTENSION); //get random new name $newName = $this->random->getRandomUniqueName() . '_' . date('Y.m.d_h:i:sa'); $newFullName = $attachmentPath . $newName . '.' . $extention; // rename rename($name, $newFullName); $uploadResult = $newFullName; return $uploadResult; }
public function testAdapterContainsFileWithName() { $this->dispatch('/learn-zf2-ajax-image-gallery/index/upload'); $this->assertContains('test1.jpg', $this->adapter->getFileName()); }
protected function validateImportFile($file) { $size = new Size(array('min' => 128, 'max' => 20480000)); // min/max bytes filesize $ext = new FileExt(array('extension' => array('xls', 'xlsx', 'csv'))); $mime = new MimeType(array('application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip', 'text/plain', 'text/csv')); $adapter = new FileHttp(); $adapter->setValidators(array($size, $ext, $mime), $file['name']); $isValid = $adapter->isValid(); if (!$isValid) { $dataError = $adapter->getMessages(); $this->flashMessenger()->addErrorMessage($this->errorImportTypeMessage . '<br>' . implode(' <br>', $dataError)); } else { $adapter->setDestination($this->getUploadPath()); if ($adapter->receive()) { $isValid = $adapter->getFileName(null, false); } else { $dataError = $adapter->getMessages(); $this->flashMessenger()->addErrorMessage($this->errorImportTypeMessage . '<br>' . implode(' <br>', $dataError)); $isValid = false; } } return $isValid; }