Novice
Novice

Reputation: 393

Laravel get file name from folder with File method

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
 }

]

Upvotes: 0

Views: 1800

Answers (1)

Novice
Novice

Reputation: 393

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);

Upvotes: 1

Related Questions