clusterBuddy
clusterBuddy

Reputation: 1554

Trying to parse files, receiving an error that has to do with SplFileInfo

I am saving an image with the current User object and even though i'm not yielding any wanted results. I am trying to parse an array of files, in that array, I am trying to get the object key that is named filename.

I currently have 2 images in this file array:

array:2 [▼
  0 => SplFileInfo {#219 ▶}
  1 => SplFileInfo {#220 ▶}
]

Each array key has an object and in that object I am looking for the filename key:

filename: "oIWVAvlOyG9yMzhhvTFoYX2wGNTa2pEth7gb228Z.png"

If it really matters, my code is on a controller on Laravel 5.4:

public function index(Request $request)
    {
        $user = Auth::user();
        $userImagesPath = public_path().'\\storage\\'.$user->email;
        $userImagesArray = File::allFiles($userImagesPath);
        $contents =[];
        $fileNameArray = [];
        foreach ($userImagesArray as $file){
            $contents[] = $file;
            foreach ($contents as $fileName){
                $fileNameArray[] = $fileName->filename;
            }
        }
        dd($fileNameArray);
        return view('sharmutaView')
            ->with('user', $user);
    }

I am getting an error: Undefined property: Symfony\Component\Finder\SplFileInfo::$filename, which is clearly telling me I ain't parsing right.

Upvotes: 0

Views: 1973

Answers (1)

Joe
Joe

Reputation: 2436

The userImagesArray is the array you provided?

Try this:

public function index(Request $request)
    {
        $user = Auth::user();
        $userImagesPath = public_path().'\\storage\\'.$user->email;
        $userImagesArray = File::allFiles($userImagesPath);
        $fileNameArray = [];
        foreach ($userImagesArray as $file){
                $fileNameArray[] = $file->getFilename();
            }
        }
        dd($fileNameArray);
        return view('sharmutaView')
            ->with('user', $user);
    }

Upvotes: 3

Related Questions