/**
  * Returns an array that matches the structure of a regular upload for a local file
  *
  * @param $file The file you want to get an upload array for.
  * @param string Name of the file to use in the upload array.
  * @return array Array that matches the structure of a regular upload
  */
 public static function fileToUploadArray($file, $filename = null)
 {
     $File = new File($file);
     if (empty($fileName)) {
         $filename = basename($file);
     }
     return ['name' => $filename, 'tmp_name' => $file, 'error' => 0, 'type' => $File->mime(), 'size' => $File->size()];
 }
 /**
  * testMimeTypeFalse method
  *
  * @expectedException \RuntimeException
  * @return void
  */
 public function testMimeTypeFalse()
 {
     $image = CORE_PATH . 'Cake/Test/TestApp/webroot/img/cake.power.gif';
     $File = new File($image, false);
     $this->skipIf($File->mime(), 'mimeType can be determined, no Exception will be thrown');
     Validation::mimeType($image, ['image/gif']);
 }
 /**
  * Check if given path is an image
  * @param  string  $path path of the image
  * @return bool       true on success
  */
 protected function _isImage($path)
 {
     $file = new File($path);
     $mime = $file->mime();
     return in_array($mime, $this->_mimeTypes);
 }
 /**
  * Gets information about the file that is being uploaded.
  * - gets the file size
  * - gets the mime type
  * - gets the extension if present
  * - sets the adapter by default to local if not already set
  * - sets the model field to the table name if not already set
  *
  * @param array|\ArrayAccess $upload
  * @param string $field
  * @return void
  */
 public function getFileInfoFromUpload(&$upload, $field = 'file')
 {
     if (!empty($upload[$field]['tmp_name'])) {
         $File = new File($upload[$field]['tmp_name']);
         $upload['filesize'] = $File->size();
         $upload['mime_type'] = $File->mime();
     }
     if (!empty($upload[$field]['name'])) {
         $upload['extension'] = pathinfo($upload[$field]['name'], PATHINFO_EXTENSION);
         $upload['filename'] = $upload[$field]['name'];
     }
 }
Exemple #5
0
 /**
  * Test mime()
  *
  * @return void
  */
 public function testMime()
 {
     $this->skipIf(!function_exists('finfo_open') && !function_exists('mime_content_type'), 'Not able to read mime type');
     $path = TEST_APP . 'webroot/img/cake.power.gif';
     $file = new File($path);
     $expected = 'image/gif';
     if (function_exists('mime_content_type') && mime_content_type($file->pwd()) === false) {
         $expected = false;
     }
     $this->assertEquals($expected, $file->mime());
 }
 /**
  * Upload function.
  *
  * @param string $fileName Original name of the tmp file.
  * @param string $filePath Full path to the tmp file.
  * @param bool $copy Whether copy or move the tmp file.
  * @return array
  */
 protected function _upload($fileName, $filePath, $copy = false)
 {
     $data = [];
     if (!file_exists($filePath)) {
         return $data;
     }
     $fileName = $this->generateUniqueFilename($fileName, $filePath);
     $basePath = $this->basePath();
     $fullPath = $basePath . DS . $fileName;
     $folder = new Folder($basePath, true, 0775);
     $transferFn = $copy ? 'copy' : 'move_uploaded_file';
     if (file_exists($fullPath) || call_user_func_array($transferFn, [$filePath, $fullPath])) {
         $file = new File($fullPath);
         if (false !== $file->size()) {
             $data = ['filename' => $fileName, 'size' => $file->size(), 'mime' => $file->mime(), 'created' => Time::now()];
         }
     }
     return $data;
 }
 /**
  * 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';
 }
 /**
  * Validates mime types.
  *
  * @deprecated Use \Cake\Utility\Validation::mimeType() instead.
  * @param array $value.
  * @param array $mimeTypes.
  * @return boolean
  */
 public function mimeType($value, $mimeTypes)
 {
     if (is_string($mimeTypes)) {
         $mimeTypes = [$mimeTypes];
     }
     $File = new File($value['tmp_name']);
     $this->_mimeType = $this->_mimeType = $File->mime();
     if (!in_array($this->_mimeType, $mimeTypes)) {
         return false;
     }
     return true;
 }