0

Cant figure out how to get file name from folder in ````filePath```

use Illuminate\Support\Facades\File;

    $filePath = storage_path('app/apiFiles/'.auth()->user()->id_message.'/');

    $fileName = File::files($filePath);

    dd($fileName);

dd($fileName) return array but i need only filename is possible to separated from array?

what i need from array is only this filename: "Z1iZ03gEhltPZj2Z2Rxnnuga2eXywheL4pQc5q0I.zip"

 array:1 [▼
    0 => Symfony\Component\Finder\SplFileInfo {#293 ▼
    -relativePath: ""
    -relativePathname: "Z1iZ03gEhltPZj2Z2Rxnnuga2eXywheL4pQc5q0I.zip"
    path: "/var/www/html/domain/storage/app/apiFiles/910960"
    filename: "Z1iZ03gEhltPZj2Z2Rxnnuga2eXywheL4pQc5q0I.zip"
    basename: "Z1iZ03gEhltPZj2Z2Rxnnuga2eXywheL4pQc5q0I.zip"
    pathname: "/var/www/html/domain/storage/app/apiFiles/910960/Z1iZ03gEhltPZj2Z2Rxnnuga2eXywheL4pQc5q0I.zip"
    extension: "zip"
    realPath: "/var/www/html/domain/storage/app/apiFiles/910960/Z1iZ03gEhltPZj2Z2Rxnnuga2eXywheL4pQc5q0I.zip"
   aTime: 2020-10-22 06:46:37
   mTime: 2020-10-22 06:46:37
   cTime: 2020-10-22 06:46:37
   inode: 1308192
   size: 3180822
   perms: 0100644
   owner: 33
   group: 33
   type: "file"
   writable: true
   readable: true
   executable: false
   file: true
   dir: false
   link: false
 }

]

5
  • use Illuminate\Support\Facades\Storage; $files = Storage::files($directory); Will it work? Commented Oct 22, 2020 at 7:33
  • @SiddharajsinhZala dd($files) return empty [] Commented Oct 22, 2020 at 7:37
  • is it possible that the folder has multiple files? Commented Oct 22, 2020 at 7:43
  • @SiddharajsinhZala only one file Z1iZ03gEhltPZj2Z2Rxnnuga2eXywheL4pQc5q0I.zip Commented Oct 22, 2020 at 7:46
  • Then you can check whether the array is empty or not? If it is not empty, then get the file name from [0] index. What is the issue? Commented Oct 22, 2020 at 7:48

1 Answer 1

1

solution, wierd but its enough for my project

 $filePath = storage_path('app/Files/'.auth()->user()->id_message.'/');

dd($filePath)return -> /var/www/html/domain/storage/app/apiFiles/910960/

in directory 910960 is one file only hash.zip

get name of file from directory:

$fileNameArray = preg_grep("~\.(zip)$~", scandir($filePath));

dd($fileNameArray) return -> array with "Z1iZ03gEhltPZj2Z2Rxnnuga2eXywheL4pQc5q0I.zip"

last step is convert array to string:

$fileNameString = implode("", $fileNameArray);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.