/** * 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'])]; } }
/** * @throws \Exception */ private static function decompressGz($compressedFile, $destinationFolder, $originalFilename = null, $allowedExtensions = []) { //IN ORDER TO WORK CORRECTLY GZ FILES MUST HAVE DOUBLE EXTENSION E.G. name.csv.gz (STANDARDIZE IT !) //$file_path = Util::getPath( // array($destinationFolder, basename(($originalFilename ? $originalFilename : $compressedFile), '.gz')) //); //File extension $file_path = Util::get_path([$destinationFolder, basename($originalFilename ? $originalFilename : $compressedFile, '.gz')]); $sfp = gzopen($compressedFile, 'rb'); if ($sfp === false) { throw new \Exception('Cannot open gz archive'); } $fp = fopen($file_path, 'w'); if ($fp === false) { throw new \Exception('Cannot open uncompress'); } while (!gzeof($sfp)) { $string = gzread($sfp, 8192); fwrite($fp, $string, strlen($string)); //TODO CHECK FOR BINARY FILES } gzclose($sfp); fclose($fp); return basename($file_path); }
/** * Delete all contents from a directory * @param string $directory Directory path * @param boolean $DELETE_DIRECTORY *[Optional]*, if is set directory will be deleted too. */ public static function deleteDirectoryContents($directory, $DELETE_DIRECTORY = false) { $files = array_diff(scandir($directory), ['.', '..']); foreach ($files as $file) { $path = Util::get_path([$directory, $file]); is_dir($path) ? self::delete_directory_contents($path, true) : unlink($path); } return $DELETE_DIRECTORY ? rmdir($directory) : true; }