jorge_dev96
3 months ago
660
4
Laravel

Save a file with the real extension of a file not tmp extension

Posted 3 months ago by jorge_dev96

I'm trying to save an image from a multiple array object, the object is like this

array(4) {
  ["question"]=>
  string(13) "safasdhrewrtj"
  ["type"]=>
  string(16) "multiple_options"
  ["options"]=>
  array(2) {
    ["option_2"]=>
    string(10) "sfafasgved"
    ["option_1"]=>
    string(17) "eggrfwefszwgzegds"
  }
  ["test_image"]=>
  object(Illuminate\Http\UploadedFile)#268 (7) {
    ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
    bool(false)
    ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
    string(39) "fabio-spinelli-28P74KO_l3k-unsplash.jpg"
    ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
    string(10) "image/jpeg"
    ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
    int(0)
    ["hashName":protected]=>
    NULL
    ["pathName":"SplFileInfo":private]=>
    string(43) "C:\Users\neo\AppData\Local\Temp\php5D7E.tmp"
    ["fileName":"SplFileInfo":private]=>
    string(11) "php5D7E.tmp"
  }
}

As you can see it is an array of n elements with a test_image as an array with an object inside

I have this foreach loop to access trouh the object

foreach($request->all()["question_group"] as $copy){
        //$this->validate($request->all()['question_group'], [
        //    'test_image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        //]);
        $imageName = uniqid().'.'.$copy["test_image"]->getClientOriginalExtension();
        $copy["test_image"]->move(public_path('/storage/',$imageName));

}

it saves the file but with a .tmp extension. And I dont know how to change it.

Please sign in or create an account to participate in this conversation.