Magento 2, You can check the given folder path contains the file or not using the isFile() method.
For example, You have an image inside the pub/media folder with path,
$imagePath = ‘/catalog/product/placeholder/default/coming-soon.jpg’;
You can see a placeholder image at the above location in the media folder.
Now you have to check coming-soon.jpg file is exists or not using Magento 2, You need to use the below code snippet,
<?php
namespace Rbj\Model\FileCheck;
use Magento\Framework\Filesystem;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem\Directory\WriteInterface;
class ImagePath
{
/**
* @var WriteInterface
*/
protected $mediaDirectory;
public function __construct(
Filesystem $filesystem
) {
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
}
public function checkImagePath()
{
$imagePath = '/catalog/product/placeholder/default/coming-soon.jpg';
if ($this->mediaDirectory->isFile($imagePath)) {
// code for file is available
}
}
coming-soon.jpg resides under the given location in the method.
You can check the given path is file exists or not using the above way bypassing the image path only.
