Ejemplo n.º 1
0
 /**
  * Upload file
  * @param array $parameters The request parameters.
  * @param string $index The paramatern index for example 'file'.
  * @param array $move If $move is set (Is not (null or == array() or false ))
  * then the uploaded file will be moved to the specified directory.
  * The $move takes indexes 'path' and 'name', the path is the directory
  * the uploaded file will be moved. The name is optional and
  * @param array $allowedFiletypes *[Optional] Array with allowed extensions.
  * @param integer $max_size *[Optional] The maximum size of the
  * uploaded file in bytes. Default value is 10485760 bytes
  * @param boolean $parseExtension *[Optional] If true the destination
  * filename will be joined with files extension  Default value is false
  */
 public static function file($file, $move = [], $allowedFiletypes = ['csv'], $max_size = 10485760, $parseExtension = false)
 {
     if (!$file) {
         return 'Select a file';
     }
     $temporaryPath = $file['tmp_name'];
     if (!file_exists($temporaryPath)) {
         throw new NotFoundException('File not found');
     }
     $filename = $file['name'];
     $ext = Util::extension($filename);
     if (!in_array($ext, $allowedFiletypes)) {
         return 'Incorrect file type';
     }
     $size = filesize($temporaryPath);
     if ($size > $max_size) {
         return 'File size exceeds maximum';
     }
     if ($move) {
         if (!is_array($move)) {
             $move['path'] = $move;
         }
         if (isset($move['name']) && $parseExtension) {
             $move['name'] .= '.' . $ext;
         } elseif ($parseExtension) {
             $move['path'] .= '.' . $ext;
         }
         $destination = Util::get_path(isset($move['name']) ? [$move['path'], $move['name']] : [$move['path']]);
         if (!rename($temporaryPath, $destination)) {
             return 'Error uploading file';
         }
         return ['path' => $destination, 'name' => basename($destination), 'size' => filesize($destination), 'name_original' => basename($file['name'])];
     } else {
         return ['path' => $temporaryPath, 'name' => basename($temporaryPath), 'size' => filesize($temporaryPath), 'name_original' => basename($file['name'])];
     }
 }
Ejemplo n.º 2
0
 /**
  * @throws \Exception
  */
 private static function decompressTar($compressedFile, $destinationFolder, $originalFilename = null, $allowedExtensions = [])
 {
     try {
         $zip = new \PharData($compressedFile);
     } catch (\Exception $e) {
         throw new \Exception('Cannot open tar archive');
     }
     $files = [];
     foreach ($zip as $file) {
         $name = $file->getFileName();
         if (in_array(Util::extension($name), $allowedExtensions)) {
             $files[] = $name;
         }
     }
     if (!$files) {
         throw new \Exception('No valid files found inside archive');
     }
     $zip->extractTo($destinationFolder, $files);
     return $files;
 }