0

I'm trying to check if file exists using storage exists() function.

This function returns true if file name doesn't have spaces but false otherwise.

I'm trying to use Storage facades code as follows

if (Storage::disk("local")->exists($model->path)) { // path of the file 
    Storage::move(); // logic to move files
}

Storage folders has all permissions

I have lot of files with spaces in my storage subfolder F1. I want to move those file to another subfolder F2 inside storage itself. It would be tedious to rename each and every file for exists() to work.


Update

Php inbuilt file_exists() works for same path i.e it returns true if file is found and even if it has spaces in it's name.

Code for file_exists()


file_exists(storage_path() . '/app/F1/Demo One.pdf');

I have used tinker for debugging.

Why storage facades exists() returns false?

Ankit.Z
  • 740
  • 8
  • 20

1 Answers1

-1

storage_path() and the local Storage disk have different base directories

storage_path() base directory is 'storage'

Storage::disk('local') base directory is 'storage/app'

ColinMD
  • 952
  • 6
  • 14
  • if I do `Storage::disk('local)->path(F1/Demo One.pdf)` and `file_exists(storage_path() . '/F1/Demo One.pdf');` both outputs have same path. Why 1st is not working but 2nd is working in if condition? – Ankit.Z Oct 08 '22 at 12:08
  • They are not the same path, the first will equate to 'storage/app/F1/Demo One.pdf' the 2nd will equate to 'storage/F1/Demo One.pdf' notice the 'app' in the first one, as the local disk route is 'storage/app' and not 'storage' – ColinMD Oct 08 '22 at 12:10
  • Sorry I mistyped `file_exists` its actually `file_exists(storage_path() . '/app/F1/Demo One.pdf');` . I'm using tinker to check output for both functions. Updated Question – Ankit.Z Oct 08 '22 at 12:19
  • `Storage::disk("local")->exists('F1/Demo one.pdf');` returns false whereas `file_exists(storage_path() . '/app/F1/Demo one.pdf');` returns true – Ankit.Z Oct 08 '22 at 12:22
  • Ok, I have just done this and both return true, both have spaces. Have you changed the default local config and is the casing the same? Storage::disk('local')->exists('F1/RR logo.png'); file_exists(storage_path() . '/app/F1/RR logo.png'); – ColinMD Oct 08 '22 at 12:32
  • I have same casing. I didn't understand default local config part.. what you mean? – Ankit.Z Oct 10 '22 at 06:08