Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AsnCode's avatar

PHP logics problem in Laravel !?

Hello i have a little problem not specificly laravel but php i think ^^

I have a Trait method return that :

function uploadMultipleImage(Request $request, string $inputName, string $path = '/uploads') : ?array
    {
        if($request->hasFile($inputName)){

            $images = $request->{$inputName};

            $paths = [];
            $sizes = [];

            foreach($images as $image){
                $ext = $image->getClientOriginalExtension();
                $size = $image->getSize();

                $imageName = 'media_' . uniqid(). '.' . $ext;

                $image->move(public_path($path), $imageName);
                $paths[] = $path . '/' . $imageName;
                $sizes[] = $size;
            }

            return ['paths' => $paths, 'sizes' => $sizes];
        }

        return null;
    }

But in my controller i want to create an Image instance but i dont know how can i destruct my array for create this instance ^^

public function store(ImagesStoreRequest $request, Capsule $capsule)
    {
        $validated = $request->validated();

        $images = $this->uploadMultipleImage($request, 'images');

        foreach ($images['paths'] as $path)
        {
            $image = new CapsuleImageGallery();
            $image->capsule_id = $validated['capsule_id'];
            $image->image = $path;
            $image->save();
        }

        return redirect()->back()->with('success', 'Images added!');
    }

public function store(ImagesStoreRequest $request, Capsule $capsule)
    {
        $validated = $request->validated();

        $images = $this->uploadMultipleImage($request, 'images');

        foreach ($images['paths'] as $path)
        {
            $image = new CapsuleImageGallery();
            $image->capsule_id = $validated['capsule_id'];
            $image->image = $path;
			$image->size  =  ????????;
            $image->save();
        }

        return redirect()->back()->with('success', 'Images added!');
    }

With the array $images['paths'] no problem .... but i dont know how to retrieve the second part : the $sizes array ?

  • HOW to structure the foreach loop for retrieves all infos : paths, sizes ... and how to create my CapsuleImageGallery instance ^^

i've put ???? in my code for better understand ^^ thanks you.

I think its an easy php logic but im stuck lol.

thanks you

0 likes
1 reply
jlrdw's avatar

Don't use getClientOriginalExtension, it's a security risk. See:

https://symfonycasts.com/screencast/symfony-uploads/file-naming#play

https://symfonycasts.com/screencast/symfony-uploads/upload-in-form#play

From Laravel docs:

However, keep in mind that the getClientOriginalName and getClientOriginalExtension methods are considered unsafe, as the file name and extension may be tampered with by a malicious user. For this reason, you should typically prefer the hashName and extension methods to get a name and an extension for the given file upload:

However getClientOriginalName can be made safe.

1 like

Please or to participate in this conversation.