protected function _upload($fileName, $filePath) { $data = []; $basePath = $this->basePath(); $pathinfo = pathinfo($fileName); $fileName = $pathinfo['basename']; $fullPath = $basePath . DS . $fileName; $folder = new Folder($basePath, true, 0777); $cpt = 1; while (file_exists($fullPath)) { $fileName = $pathinfo['filename'] . '_' . $cpt . '.' . $pathinfo['extension']; $fullPath = $basePath . DS . $fileName; $cpt++; } if (move_uploaded_file($filePath, $fullPath)) { $file = new File($fullPath); $data = ['path' => $this->basePath(true) . DS . $file->name, 'extension' => $file->ext()]; } return $data; }
/** * Setup for display or download the given file. * * If $_SERVER['HTTP_RANGE'] is set a slice of the file will be * returned instead of the entire file. * * ### Options keys * * - name: Alternate download name * - download: If `true` sets download header and forces file to be downloaded rather than displayed in browser * * @param string $path Path to file. If the path is not an absolute path that resolves * to a file, `APP` will be prepended to the path. * @param array $options Options See above. * @return void * @throws \Cake\Network\Exception\NotFoundException */ public function file($path, array $options = []) { $options += ['name' => null, 'download' => null]; if (strpos($path, '../') !== false || strpos($path, '..\\') !== false) { throw new NotFoundException('The requested file contains `..` and will not be read.'); } if (!is_file($path)) { $path = APP . $path; } $file = new File($path); if (!$file->exists() || !$file->readable()) { if (Configure::read('debug')) { throw new NotFoundException(sprintf('The requested file %s was not found or not readable', $path)); } throw new NotFoundException(__d('cake', 'The requested file was not found')); } $extension = strtolower($file->ext()); $download = $options['download']; if ((!$extension || $this->type($extension) === false) && $download === null) { $download = true; } $fileSize = $file->size(); if ($download) { $agent = env('HTTP_USER_AGENT'); if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent)) { $contentType = 'application/octet-stream'; } elseif (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) { $contentType = 'application/force-download'; } if (!empty($contentType)) { $this->type($contentType); } if ($options['name'] === null) { $name = $file->name; } else { $name = $options['name']; } $this->download($name); $this->header('Content-Transfer-Encoding', 'binary'); } $this->header('Accept-Ranges', 'bytes'); $httpRange = env('HTTP_RANGE'); if (isset($httpRange)) { $this->_fileRange($file, $httpRange); } else { $this->header('Content-Length', $fileSize); } $this->_clearBuffer(); $this->_file = $file; }
/** * Return the type from a File object * * @param File $file The file from which you get the type * @return string */ protected function getType($file) { $extension = $file->ext(); if (isset($this->typeMap[$extension])) { return $this->typeMap[$extension]; } return $file->mime() ?: 'application/octet-stream'; }
/** * Clean the generated CSS files * * @return array */ public function cleanGeneratedCss() { $this->_removeCacheKey('sass_compiled'); // Cleaned files that we will return $cleanedFiles = array(); foreach ($this->_sassFolders as $key => $sassFolder) { foreach ($sassFolder->find() as $file) { $file = new File($file); if (($file->ext() == 'sass' || $file->ext() == 'scss') && substr($file->name, 0, 2) !== '._') { $sassFile = $sassFolder->path . DS . $file->name; $cssFile = $this->_cssFolders[$key]->path . DS . str_replace(array('.sass', '.scss'), '.css', $file->name); if (file_exists($cssFile)) { unlink($cssFile); $cleanedFiles[] = $cssFile; } } } } // Remove all cache files at once if (is_dir($this->_cacheFolder)) { @closedir($this->_cacheFolder); $folder = new Folder($this->_cacheFolder); $folder->delete(); unset($folder); $cleanedFiles[] = $this->_cacheFolder . DS . '*'; } mkdir($this->_cacheFolder); return $cleanedFiles; }
/** * Copy Objednavku * @param $id */ public function copy($id) { $row = $this->Orders->findById($id)->contain(["Accessories", "Images", "Files", "Statuses", "Operations"])->first(); if (!$row) { throw new InternalErrorException("Chyba pri nacitani objednavky", 500); } try { $data = $row->toArray(); // Copy All Images if ($data['images']) { foreach ($data['images'] as $key => $image) { $file_name = new File($image['name']); $new_file = md5($file_name->name() . time()) . "." . $file_name->ext(); if (copy('img/images/' . $file_name->name, 'img/images/' . $new_file)) { $data['images'][$key]['name'] = $new_file; } } } // Copy all Files if ($data['files']) { foreach ($data['files'] as $key => $file) { $file_name = new File($file['name']); $origin_file = preg_replace("/-copy-\\d{2}\\.\\d{2}\\.\\d{2}.\\d{2}.\\d{2}/", "", $file_name->name()); $new_file = $origin_file . "-copy-" . date("d.m.y-H-i") . "." . $file_name->ext(); if (copy('files/' . $file_name->name, 'files/' . $new_file)) { $data['files'][$key]['name'] = $new_file; } } } $order = $this->Orders->newEntity($data); $order->user_id = $this->getUserId(); if (!$this->Orders->save($order)) { $message = ['message' => "error saving data", "data" => $order->errors()]; throw new InternalErrorException(json_encode($message)); } } catch (Exception $e) { throw new InternalErrorException($e->getMessage()); } $this->set('row', $order); $this->set('_serialize', ['row']); }