/** * {@inheritDoc} * * @return Stream|Response */ public function indexAction() { $response = $this->getResponse(); $params = $this->params()->fromRoute(); if ($params['path'] === '') { $response->setStatusCode(404); return $response; } $info = pathinfo($params['path']); if ($info['dirname'] === '.') { $info['dirname'] = ''; } /* @var $folder \CmsFile\Mapping\FolderInterface */ $folder = $this->getFolderService()->getMapper()->findOneByPath('/' . trim($info['dirname'], '/')); if (!$folder) { $response->setStatusCode(404); return $response; } $file = $folder->getFile($info['basename']); if (!$file) { $response->setStatusCode(404); return $response; } $response = new Stream(); $response->setStream(fopen($file, 'r')); $response->setStatusCode(200); $headers = new Headers(); $headers->addHeaderLine('Content-Type', $file->getType())->addHeaderLine('Content-Disposition', 'attachment; filename="' . $file->getTitle() . '"')->addHeaderLine('Content-Length', filesize($file)); $response->setHeaders($headers); return $response; }
public function downloadErrorsAction() { $date = $this->params('date'); $path = sprintf('data/logs/php_log.%s.xml', $date); $response = new Stream(); $response->setStream(fopen($path, 'r')); $response->setCleanup(false); $response->setStatusCode(200); $headers = $response->getHeaders(); $headers->addHeaderLine(sprintf('Content-Disposition: attachment; filename="log-%s.csv"', $date)); $headers->addHeaderLine('Content-Type: text/csv'); return $response; }
/** * Get file stream response * * * @access public * @param string $file * @return Stream */ public function getFileResponse($file) { $response = new Response(); if (!empty($file) && file_exists($file)) { $response = new Stream(); $response->setStream(fopen($file, 'r')); $response->setStatusCode(200); $response->setStreamName(basename($file)); $headers = new Headers(); $headers->addHeaders(array('Content-Disposition' => 'attachment; filename="' . basename($file) . '"', 'Content-Type' => 'application/octet-stream', 'Content-Length' => filesize($file), 'Expires' => '@0', 'Cache-Control' => 'must-revalidate', 'Pragma' => 'public')); $response->setHeaders($headers); } return $response; }
public function stream($file, $type) { switch ($type) { case "css": $mimeType = 'text/css'; break; case 'js': $mimeType = 'text/javascript'; break; default: $mimeType = mime_content_type($file); } $response = new Stream(); $response->setStream(fopen($file, 'r')); $response->setStatusCode(200); $response->setStreamName(basename($file)); $headers = new Headers(); $headers->addHeaders(array('Content-Type' => $mimeType, 'Content-Length' => filesize($file))); $response->setHeaders($headers); return $response; }
/** * Send header to download the given file * * @param int $fileID * @return \Zend\Http\Response\Stream */ protected function downloadFile($fileID) { $file = $this->getFileTable()->getFile($fileID); $this->getFileTable()->incrementDownloadCount($file); $fileUrl = $this->getOptions()->getDownloadFolderPath() . "/" . $file->url; if (!file_exists($fileUrl)) { throw new \Exception("File doesn't exists"); } $response = new Stream(); $response->setStream(fopen($fileUrl, 'r')); $response->setStatusCode(200); $headers = new Headers(); $headers->addHeaderLine('Content-Type', 'application/octet-stream')->addHeaderLine('Content-Disposition', 'attachment; filename="' . $file->name . '"')->addHeaderLine('Cache-Control', 'must-revalidate')->addHeaderLine('Pragma', 'public')->addHeaderLine('Content-Length', filesize($fileUrl)); $response->setHeaders($headers); return $response; }
public function downloadFile($filePath, $filename = FALSE) { $name = $filename ?: basename($filePath); $response = new Stream(); $response->setStream(fopen($filePath, 'r')); $response->setStatusCode(200); $response->setStreamName(basename($filePath)); $headers = new Headers(); $headers->addHeaders(array('Content-Disposition' => "attachment; filename='{$name}'", 'Content-Type' => 'application/octet-stream', 'Content-Length' => filesize($filePath), 'Expires' => '@0', 'Cache-Control' => 'must-revalidate', 'Pragma' => 'public')); $response->setHeaders($headers); return $response; }
/** * @param $file * @return Stream */ function forceDownloadAction($file) { // $file = '/path/to/my/file.txt'; $file = $_SERVER['DOCUMENT_ROOT'] . '/' . $file; $response = new Stream(); $response->setStream(fopen($file, 'r')); $response->setStatusCode(200); $response->setStreamName(basename($file)); $headers = new Headers(); $headers->addHeaders(array('Content-Disposition' => 'attachment; filename="' . basename($file) . '"', 'Content-Type' => 'application/octet-stream', 'Content-Length' => filesize($file))); $response->setHeaders($headers); return $response; }
public function downloadFileAction() { $fileName = urldecode($this->params()->fromRoute("fileName", null)); echo ROOT_PATH . '/' . $fileName; var_dump(file_exists(ROOT_PATH . '/' . $fileName)); if (!$fileName || !file_exists(ROOT_PATH . '/' . $fileName)) { return $this->notFoundAction(); } else { $pathInfo = pathinfo($fileName); switch ($pathInfo['extension']) { case "pdf": $type = "application/pdf"; break; case "xls": $type = "application/vnd.ms-excel"; break; case "zip": $type = "application/zip"; break; case "ppt": $type = "application/vnd.ms-powerpoint"; break; default: $type = "text/plain"; } $response = new Stream(); $response->setStream(fopen($fileName, 'r')); $response->setStatusCode(200); $headers = new \Zend\Http\Headers(); $headers->addHeaderLine('Content-Type', $type)->addHeaderLine('Content-Disposition', 'attachment; filename="' . $fileName . '"')->addHeaderLine('Content-Length', filesize($fileName)); $response->setHeaders($headers); return $response; } }
public function viewFileAttachmentAction() { $file_no = (int) $this->params()->fromRoute('id', 0); if (!$file_no) { return $this->redirect()->toRoute('app', array('controller' => 'failed', 'action' => 'forbidden')); } $currentUser = $this->auth()->get('user_no'); $dbAdapter = $this->getDb(); $sql = "SELECT \n t_decision_file.file_no, \n t_decision_file.decision_no, \n t_decision_file.file_name, \n t_decision_file.content_type, \n OCTET_LENGTH( t_decision_file.binary_data) as file_size, \n t_decision_file.binary_data,\n t_decision.create_user\n FROM t_decision_file \n LEFT JOIN t_decision \n ON t_decision_file.decision_no = t_decision.decision_no\n WHERE t_decision_file.file_no = {$file_no}"; $result = $dbAdapter->query($sql)->execute(); $file = $result->current(); if (!$file) { return $this->redirect()->toRoute('app', array('controller' => 'failed', 'action' => 'not-found')); } $continue = true; // you must be the create user of the request if ($file['create_user'] != $currentUser) { $continue = false; } // if you are not the create_user of the request you must be 1 of approver if (!$continue) { // check file restriction $result2 = $dbAdapter->query("SELECT\n t_decision_approval.user_no\n FROM t_decision_approval\n WHERE t_decision_approval.decision_no = {$file['decision_no']}")->execute(); $resultSet = new ResultSet(); $resultSet->initialize($result2); $resultAr = $resultSet->toArray(); if (is_array($resultAr)) { foreach ($resultAr as $rec) { if ((int) $rec['user_no'] === (int) $currentUser) { $continue = true; break; } } } } // Otherwise you will not be able to access this attachment if (!$continue) { $this->flashMessenger()->addMessage("You don't have enough access level to view this document"); return $this->redirect()->toRoute('app', array('controller' => 'failed', 'action' => 'forbidden')); } $file_name = $file["file_name"]; $file_size = $file["file_size"]; $file_mime_type = $file["content_type"]; $file_content = $file["binary_data"]; $new_filename = "temp_" . $file_name; $filePath = APP_UPLOAD_DIR . $new_filename; \file_put_contents($filePath, $file_content); $response = new Stream(); $response->setStream(fopen($filePath, 'r')); $response->setStatusCode(200); $response->setStreamName(basename($filePath)); $headers = new Headers(); $headers->addHeaders(array('Content-Disposition' => 'inline; filename="' . $new_filename . '"', 'Content-Type' => $file_mime_type . ';charset=UTF-8', 'Content-Length' => $file_size)); $response->setHeaders($headers); /**$view = new ViewModel(array( 'response' => $response, 'headers' => $headers )); $view->setTemplate('/common/view-attachment.phtml'); $view->setTerminal(true);**/ // return $view; return $response; }
/** * @param MvcEvent $event */ public function onDispatchError(MvcEvent $event) { if (Application::ERROR_ROUTER_NO_MATCH != $event->getError()) { // ignore other than 'no route' errors return; } // get URI stripped of a base URL $request = $event->getRequest(); $uri = str_replace($request->getBaseUrl(), '', $request->getRequestUri()); // try get image ID from URI $id = $this->manager->matchUri($uri); if (!$id) { // abort if URI does not match return; } // try get image from repository $image = $this->repository->find($id); if (!$image) { // abort if image does not exist return; } // store image $this->manager->store($image); // return image in response as a stream $headers = new Headers(); $headers->addHeaders(['Content-Type' => $image->getType(), 'Content-Length' => $image->getLength()]); $response = new Stream(); $response->setStatusCode(Response::STATUS_CODE_200); $response->setStream($image->getResource()); $response->setStreamName($image->getName()); $response->setHeaders($headers); $event->setResponse($response); }