I am checking uploaded file extension by pathinfo() function of php but in coding standard check it's shows

The use of function pathinfo() is discouraged

My code:

$fileExt = pathinfo($file, PATHINFO_EXTENSION);
  • Let me know if you have any issue – Prince Patel Sep 12 '17 at 18:40

You can use Magento\Framework\Filesystem\Io\File instead.



namespace Vendor\ModuleName\Model;

use Magento\Framework\Filesystem\Io\File;

class MyClass
    * @var File
   private $file;

    * @param File $file
   public function __construct(File $file)
       $this->file = $file;

   public function myMethod()
       $path = 'this\is\some\dummy\path\potato.jpg';
       $pathInfo = $this->file->getPathInfo($path);

Use \Magento\MediaStorage\Model\File\UploaderFactory for file operation You can get file extension by this code:

$uploader = $this->fileUploaderFactory->create(['fileId' => $scope]);

if ($uploader->save($this->getBaseDir())) {
    echo $fileName = $uploader->getUploadedFileName();
    echo $fileExt = $uploader->getFileExtension();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.