public function indexAction() { $request = $this->getRequest(); if ($request->ext == 'mp3') { $table = new Model_DbTable_Recordings(); $select = $table->select()->where('id = ?', $request->id); $path = UPLOAD_PATH . '/recordings/' . $request->id . '.mp3'; if (($row = $table->fetchRow($select)) && file_exists($path)) { $pattern = array('/ - /', '/\\s+/'); $replace = array('-', '_'); $filename = preg_replace($pattern, $replace, $row->title) . '.mp3'; $this->_helper->sendFile($path, 'audio/mpeg', array('filename' => $filename)); } } elseif (in_array($request->ext, array('pdf', 'mid'))) { $table = new Model_DbTable_Downloads(); $select = $table->select()->where('id = ?', $request->id)->where('type = ?', $request->ext == 'pdf' ? 'PDF' : 'MIDI'); $path = UPLOAD_PATH . '/sheets/' . $request->id . '.' . $request->ext; $mime = $request->ext == 'pdf' ? 'application/pdf' : 'audio/midi'; if (($row = $table->fetchRow($select)) && file_exists($path)) { $pattern = array('/ - /', '/\\s+/'); $replace = array('-', '_'); $filename = preg_replace($pattern, $replace, $row->title) . '.' . $request->ext; $this->_helper->sendFile($path, $mime, array('filename' => $filename)); } } exit; }
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; }