You can check any physical file is exist or not at a specific location in Magento directory by Magento\Framework\Filesystem\Driver\File class.
You can use Magento 2 native class to check file is exist or not instead of the core PHP method.
I have taken a Block file and create a function for check file is exist or not via constructor DI,
<?php
namespace Rbj\FileCheck\Block;
class Demo extends \Magento\Framework\View\Element\Template
{
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Filesystem\Driver\File $fileDriver,
array $data = []
) {
$this->fileDriver = $fileDriver;
parent::__construct($context,$data);
}
/**
* Check file is exist or not at specific location
*/
public function checkFileExists()
{
$fileName = '/var/www/html/mage231/var/log/system.log';
if ($this->fileDriver->isExists($fileName)) {
return "file is exist";
} else {
return "file not exist";
}
}
Call from a template file,
echo $block->checkFileExists();
You need to pass file path in the above function to check file is available or not.
I have passed the filename as system.log from var folder. You can add any file name you want to check for file exists or not.
