Home > Software engineering >  laravel8 how to replace file original name with two requested inputs names
laravel8 how to replace file original name with two requested inputs names

Time:03-26

I have this code that changes the file's original name to the custom name from my form. I would also like to add another request in order for my file name to be composed out of 2 inputs.

 if ($request->hasFile('talonphoto')){
                $extension = $request->file('talonphoto')->getClientOriginalExtension();
                $photoName = str_replace('','', $request->name). '_'. time(). '.' .$extension;
                $request->file('talonphoto')->move('image/documentemasini', $photoName);
                $tagacars->talonphoto = $photoName;
            }

This is the code, so in order for me to change the name with two inputs would it be something similar to this?

if ($request->hasFile('talonphoto')){
                $extension = $request->file('talonphoto')->getClientOriginalExtension();
                $photoName = str_replace('','', $request->name, $request->talondata0). '_'. time(). '.' .$extension;
                $request->file('talonphoto')->move('image/documentemasini', $photoName);
                $tagacars->talonphoto = $photoName;
            }
            

CodePudding user response:

You can not put to string in str_replace function. Try this code:

$photoName = str_replace('','', $request->name). '_'.str_replace('','', $request->talondata0).'_'. time(). '.' .$extension;

CodePudding user response:

1st way you can concatenate two string request.

$photoName = str_replace('','', $request->name.$request->talondata0). '_'. time(). '.' .$extension;

2nd way you can use str_replace individual

$photoName = str_replace('','', $request->name). '_'.str_replace('','', $request->talondata0).'_'. time(). '.' .$extension;

Also use this in controller before the controller class

use Illuminate\Support\Str;
  • Related