Beispiel #1
0
 /**
  * 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\Error\NotFoundException
  */
 public function file($path, array $options = array())
 {
     $options += array('name' => null, 'download' => null);
     if (strpos($path, '..') !== false) {
         throw new Error\NotFoundException(__d('cake_dev', '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 Error\NotFoundException(sprintf('The requested file %s was not found or not readable', $path));
         }
         throw new Error\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('Accept-Ranges', 'bytes');
         $this->header('Content-Transfer-Encoding', 'binary');
         $httpRange = env('HTTP_RANGE');
         if (isset($httpRange)) {
             $this->_fileRange($file, $httpRange);
         } else {
             $this->header('Content-Length', $fileSize);
         }
     } else {
         $this->header('Content-Length', $fileSize);
     }
     $this->_clearBuffer();
     $this->_file = $file;
 }
Beispiel #2
0
 /**
  * testCreate method
  *
  * @return void
  */
 public function testCreate()
 {
     $tmpFile = TMP . 'tests/cakephp.file.test.tmp';
     $File = new File($tmpFile, true, 0777);
     $this->assertTrue($File->exists());
 }
 /**
  * Write the files that need to be stored
  *
  * @return void
  */
 protected function _writeFiles()
 {
     $overwriteAll = false;
     if (!empty($this->params['overwrite'])) {
         $overwriteAll = true;
     }
     foreach ($this->_storage as $category => $domains) {
         foreach ($domains as $domain => $sentences) {
             $output = $this->_writeHeader();
             foreach ($sentences as $sentence => $header) {
                 $output .= $header . $sentence;
             }
             $filename = $domain . '.pot';
             if ($category === 'LC_MESSAGES') {
                 $File = new File($this->_output . $filename);
             } else {
                 new Folder($this->_output . $category, true);
                 $File = new File($this->_output . $category . DS . $filename);
             }
             $response = '';
             while ($overwriteAll === false && $File->exists() && strtoupper($response) !== 'Y') {
                 $this->out();
                 $response = $this->in(__d('cake_console', 'Error: %s already exists in this location. Overwrite? [Y]es, [N]o, [A]ll', $filename), ['y', 'n', 'a'], 'y');
                 if (strtoupper($response) === 'N') {
                     $response = '';
                     while (!$response) {
                         $response = $this->in(__d('cake_console', "What would you like to name this file?"), null, 'new_' . $filename);
                         $File = new File($this->_output . $response);
                         $filename = $response;
                     }
                 } elseif (strtoupper($response) === 'A') {
                     $overwriteAll = true;
                 }
             }
             $File->write($output);
             $File->close();
         }
     }
 }
Beispiel #4
0
 /**
  * Creates a file at given path
  *
  * @param string $path Where to put the file.
  * @param string $contents Content to put in the file.
  * @return bool Success
  * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::createFile
  */
 public function createFile($path, $contents)
 {
     $path = str_replace(DS . DS, DS, $path);
     $this->_io->out();
     if (is_file($path) && empty($this->params['force'])) {
         $this->_io->out(__d('cake_console', '<warning>File `%s` exists</warning>', $path));
         $key = $this->_io->askChoice(__d('cake_console', 'Do you want to overwrite?'), ['y', 'n', 'q'], 'n');
         if (strtolower($key) === 'q') {
             $this->_io->out(__d('cake_console', '<error>Quitting</error>.'), 2);
             return $this->_stop();
         } elseif (strtolower($key) !== 'y') {
             $this->_io->out(__d('cake_console', 'Skip `%s`', $path), 2);
             return false;
         }
     } else {
         $this->out(__d('cake_console', 'Creating file %s', $path));
     }
     $File = new File($path, true);
     if ($File->exists() && $File->writable()) {
         $data = $File->prepare($contents);
         $File->write($data);
         $this->_io->out(__d('cake_console', '<success>Wrote</success> `%s`', $path));
         return true;
     }
     $this->_io->err(__d('cake_console', '<error>Could not write to `%s`</error>.', $path), 2);
     return false;
 }