/** * Handler for files uploader * @return array */ private function _uploadFiles($savePath = null) { $this->_uploadHandler->clearValidators(); $this->_uploadHandler->clearFilters(); if (!$savePath) { $savePath = $this->_getSavePath(); } $fileInfo = $this->_uploadHandler->getFileInfo(); $file = reset($fileInfo); preg_match('~[^\\x00-\\x1F"<>\\|:\\*\\?/]+\\.[\\w\\d]{2,8}$~iU', $file['name'], $match); if (!$match) { return array('result' => 'Corrupted filename', 'error' => true); } $this->_uploadHandler->addFilter('Rename', array('target' => $savePath . DIRECTORY_SEPARATOR . $file['name'], 'overwrite' => true)); if ($this->_uploadHandler->isUploaded() && $this->_uploadHandler->isValid()) { try { $this->_uploadHandler->receive(); } catch (Exceptions_SeotoasterException $e) { $response = array('result' => $e->getMessage(), 'error' => true); } } $response = array('result' => $this->_uploadHandler->getMessages(), 'error' => !$this->_uploadHandler->isReceived()); return $response; }
public function uploadAction() { $request = $this->getRequest(); $table = new Model_DbTable_Sheets(); $select = $table->select(); $select->where('id = ?', $request->id); $sheet = $table->fetchRow($select); if (!$this->view->authenticated || $sheet->user_id != $this->view->identity->id) { $this->_redirector->gotoRoute(array(), 'login'); } if ($request->isPost()) { $path = UPLOAD_PATH . '/sheets/'; if (!file_exists($path)) { mkdir(str_replace('//', '/', $path), 0755, true); } $upload = new Zend_File_Transfer_Adapter_Http(); $upload->setDestination($path); $upload->addValidators(array(array('Count', false, 5), array('Size', false, '2MB'), array('Extension', false, array('pdf', 'midi', 'mid')))); if ($upload->isValid()) { $table = new Model_DbTable_Downloads(); $pattern = array('/-/', '/_/', '/\\s+/'); $replace = array(' - ', ' ', ' '); foreach ($upload->getFileInfo() as $file => $contents) { $info = pathinfo($contents['name']); $row = $table->createRow(); $row->sheet_id = $sheet->id; $row->type = strtolower($info['extension']) == 'pdf' ? 'PDF' : 'MIDI'; $title = preg_replace($pattern, $replace, $info['filename']); $row->title = ucwords($title); $row->save(); $name = $row->id . ($row->type == 'MIDI' ? '.mid' : '.pdf'); $upload->addFilter('Rename', array('target' => $name), $file); $upload->receive($file); } if ($upload->isReceived()) { return $this->_redirector->gotoRoute(array('controller' => 'sheet', 'id' => $request->id), 'view'); } } $this->view->errors = $upload->getMessages(); } $this->view->headTitle('Upload sheet file(s)'); $this->view->headScript()->appendFile('/js/multifile.js'); $this->view->headScript()->appendFile('/js/upload.js'); $this->view->sheetId = $request->id; }
<?php // creating the File_Transfer instance (with Http Adapter) $upload = new Zend_File_Transfer_Adapter_Http(); // firelog($upload->getFileInfo()); // setting the destination $UPLOAD_DIR = $_POST['path']; $upload->setDestination($UPLOAD_DIR); // adding some validators //$upload->addValidator('Extension', false, 'csv');*/ // uploading the file if valid if ($upload->isValid()) { try { $upload->addFilter('Rename', array('target' => $UPLOAD_DIR . '/' . $upload->getFileName(null, false))); $upload->receive(); if ($upload->isReceived()) { $return = array('success' => true, 'message' => 'SUCESS: ' . $upload->getFileName(null, false) . '(' . $upload->getFileSize() . ')'); } else { $return = array('success' => false, 'error' => 'ERROR: File Not Received'); } } catch (Exception $e) { $return = array('success' => false, 'error' => 'ERROR: Upload Failure'); } } else { $return = array('success' => false, 'error' => 'ERROR: File not valid'); } echo Zend_Json::encode($return);