ArchStanton's avatar

Laravel Storage - file exists

Hi,

How can I make my file overwrite a file with the same name

Storage::disk('public')->move('uploads/'.$filename, 'uploads/'.$filename_pieces[0].'/'.$filename);

I currently get the error

FileExistsException
File already exists at path

``


0 likes
5 replies
Cronix's avatar
$file = 'uploads/'.$filename_pieces[0].'/'.$filename;

if (Storage::exists($file)) {
    Storage::delete($file);
}

Storage::disk('public')->move('uploads/'.$filename, $file);
13 likes
InfoRR's avatar

If directory is exists and $filename passed as null it would still return true. Is there away to check the file only excluding directory structure?

fendi_s0709's avatar

@InfoRR You could use getMetaData(). But, please take a note that getMetaData() will throw FileNotFoundException if file or directory is not found.

Here's what you need to do:

  1. Check is file or directory exists using exists()
  2. If it's true, check using getMetaData().
    • It will return false, if it's a directory.
    • It will return array, if it's a file. .
$filePath = 'upload/users/1';

if (Storage::exists($filePath)) {
    $metaData = Storage::getMetaData($filePath);
    if($metaData == false) {
        return false;  // It is a directory, not a file
    }
    return true; // It is a file
}

Here's example of array return from getMetaData(), if it's a file

// $filePath = 'upload/users/1/profile.jpg';
[
     "path" => "upload/users/1/profile.jpg",
     "dirname" => "upload/users/1",
     "basename" => "profile.jpg",
     "extension" => "jpg",
     "filename" => "profile",
     "timestamp" => 1626481250,
     "size" => 28362,
     "mimetype" => "image/jpeg",
     "metadata" => [],
     "storageclass" => "",
     "etag" => ""09a8cf01b4c76f4dfff7d23bed16bca0"",
     "versionid" => "jL1PoEZ.R5qcZEViL6IFLLJr5iEIEbVo",
     "type" => "file",
   ]
rktaxali's avatar

I am using Laravel 9. When I use the storage on local filesystem on my Mac, Storage::exists() works, however, it does not work on AWS S3 storage. Here is the output from the tinker session on the local system.

$path = "public/documents/47/mCzPh9USfoCnsRdt6xZcSsvY1YAzIRu3HuUChPpJ.pdf"; => "public/documents/47/mCzPh9USfoCnsRdt6xZcSsvY1YAzIRu3HuUChPpJ.pdf" Storage::disk('local')->exists( $path); => true

Storage::delete($path); => true

And here is the output from the AWS S3 storage:

use Illuminate\Support\Facades\Storage; $path = 'client/documents/178/gfPAVrp2vBnKAAQ5xQ6NL4HvG9RQN3cJ0PtzBFNw.jpg'; => "client/documents/178/gfPAVrp2vBnKAAQ5xQ6NL4HvG9RQN3cJ0PtzBFNw.jpg" Storage::disk('s3')->exists($path); => false Storage::disk('s3')->exists('client/documents/178'); => true Storage::disk('s3')->delete($path); => true

Notice that the exists() fails for the file, though it works for the folder. Also, delete() works, (I could confirm that the file has been deleted by looking at the AWS S3 console.)

Is there any reason that exists() does not work on AWS S3?

Please or to participate in this conversation.