Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ElmarHilber's avatar

Check if a given path is an directory or a file on a given disk

In filesystems.php I have configured 2 disk, one local and one on amazon: 'public' => [ 'driver' => 'local',

's3' => [ 'driver' => 's3',

I'm looking for a method that returns wheather the passed path on a given disk is a directory or not. Something like: $isDir = Storage::disk('s3')->isDirectory('path/to/directory');

I found the following but i didn't found an example that worked: is_dir($thePath) works only on locale files but not on s3 if I'm not wrong laravel.com/api/10.x/Illuminate/Filesystem/Filesystem.html#method_isDirectory laravel.com/api/10.x/Illuminate/Support/Facades/File.html#method_isDirectory

Dose something like that exists?

I tired also to write my own method, that checks if a corresponding folder exists, but this is not so nice in my eyes:

private function isFolder($disk, $folder) { $sourceParentFolder = dirname($folder); $directories = Storage::disk($disk)->directories($sourceParentFolder); return in_array($folder, $directories); }

0 likes
6 replies
ElmarHilber's avatar

I used is_file for files on local disk. But using Laravel with a disk on Amazon S3 (Storage::disk('s3')) is_file does not work as much as I have read.

Bogey's avatar

@jlrdw Yeah, for some reason all the results are in the direction of whether the directory exists rather than if it is a file or a directory

Bogey's avatar

Storage::mimeType('/public/1/image') will return the mimetype for files and false for directories...

Also Storage::directoryExists('/public/1/image') returns true for directories and false for files even if files exist and the same goes for Storage::fileExists('/public/1/image/hjk234354.jpg')... returns true for existing file and false for existing directory.

Using that, the following would work.

$path = '/public/1/image/LkKRSvH38cI.jpg';
if(Storage::directoryExists($path) && !Storage::fileExists($path)) {
    echo "$path is a directory";
} elseif(!Storage::directoryExists($path) && Storage::fileExists($path)) {
    echo "$path is a file";
} else {
    echo "$path doesn't exist";
}
chaudigv's avatar

@elmarhilber, I was looking for a similar solution where a path is given. That path could be a directory or a file path. If it's a directory, I would like all the files within it that match the specified extension. If the directory does not exist or is empty, then check whether the path is a file that supports the extension. If true, return the path as an array. Otherwise, throw an error.

public function getFilePathsFromS3(string $disk, string $path): string|array
{
    $disk = Storage::disk($disk);

    return collect($disk->files($path))
        ->filter(fn ($file) => $this->hasSupportedFormat($file))
        ->whenEmpty(function () use ($disk, $path) {
            if ($disk->exists($path) && $this->hasSupportedFormat($path)) {
                return collect([$path]);
            }

            throw new Exception('No files found with expected formats: ' . implode(', ', $this->supportedFormats));
        })
        ->values()
        ->all();
}

protected function hasSupportedFormat(string $file): bool
{
    return collect($this->supportedFormats)->contains(fn ($format) => str_ends_with($file, $format));
}

Please or to participate in this conversation.