Verify Filename has extension .tar or not using PHP Magento 2.
If the file is a valid tar file, the result will be true otherwise result will be false. Some time you need to verify the current file is tar or not in that scenario, this article is used for your development.
.tar stands for Tape Archiver (tar is shorter form of tape archiver)
If filename is data.zip, The result will be false due to .zip is not valid tar.
If filename is data.tar, its valid tar file and result will be true.
<?php namespace Jesadiya\Tar\Model; use Magento\Framework\Archive; class VerifyTar { /** * @var Archive */ public $archive; public function __construct( Archive $archive ) { $this->archive = $archive; } public function verifyTar() { // Result true $fileName = 'test.tar'; if ($this->archive->isTar($fileName)) { // call custom function here... } } }
Call method, $this->verifyTar() and pass valid tar file as filename.
Output: Boolean
If Valid Tar file, true otherwise result will be false.