Zaty's avatar
Level 1

how to upload file from content-disposition in laravel/php

I'm trying to upload file that I received from json that include content-disposition in header. My problem is the file is not detected and not uploaded to the server.

[headers] => Array
(
     ([namesAndValues] => Array
     (
         [0] => Content-Disposition
         [1] => form-data; name="certificateFile"; filename="2022-12-01-20-58-40-247.jpeg"
      )
)
0 likes
2 replies
vincent15000's avatar

What is your code that send the JSON file ?

What is your code that uploads the file ?

Zaty's avatar
Level 1

@vincent15000 The code that send the json file is from another programmer (outsource from other company), so I don't have it. He send the response data through api from android to laravel.

Here my code to upload the file

foreach($value['certificatePicture']['headers'] as $key1=>$value1)
{
         if (sscanf($value1[1], 'form-data; name="%[^"]"; filename="%[^"]"', $name, $filename)) 
        {
               //echo $name . "\n" . $filename. "\n";
  
               //$name2 = base64_encode(file_get_contents($_FILES[$filename]));
         
                $attach = 'no';
                if($request->hasFile($filename))
                {
                         $file = $request->file($filename);
                        
                         $filename = time().'_'.$file->getClientOriginalName();

                         // File extension
                         $extension = $file->getClientOriginalExtension();
                         $imageSize = $file->getSize();

                          // File upload location
                         $location = 'files/talents';

                         // Upload file
                         $file->move($location,$filename);
                            
                         // File path
                         $filepath_mykad = url('files/talents/'.$filename);

                         $attach = 'yes';
                }
                    
                Project::where('id', 1)
               ->update([
                       'request_all' => $attach
               ]); 
         }
 } 

Please or to participate in this conversation.