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

ravipw1801's avatar

Image file name with space shows error in URL

User can upload images for their post & the image name may contain spaces. So while seeing this in front end, the spaces are replaced by %20 which shows invalid propery value.

So my question is, how can I save the image in database without spaces or the spaces replaced with any character (for eg.: hyphen or underscore) .

Below is my controller, saving images

if ($request->hasFile('logoimg')) {
            $logoimgwithExt = $request->file('logoimg')->getClientOriginalName();
            $logoimgFileName = pathinfo($logoimgwithExt, PATHINFO_FILENAME);
            $extension = $request->file('logoimg')->getClientOriginalExtension();
            $logoFileNameToStore = $logoimgFileName.'_'.time().'.'.$extension;
            $path = $request->file('logoimg')->storeAs('public/logo_images',$logoFileNameToStore);            
        } else {
          $logoFileNameToStore = 'no-logo.jpg';   
        }
0 likes
2 replies

Please or to participate in this conversation.