image file upload via postman in laravel api

Solutions on MaxInterview for image file upload via postman in laravel api by the best coders in the world

showing results for - "image file upload via postman in laravel api"
Christopher
24 May 2017
1<?php
2 
3namespace App\Http\Controllers\API;
4 
5use App\Http\Controllers\Controller;
6 
7use App\Models\Image;
8 
9use Validator;
10 
11use Illuminate\Http\Request;
12 
13class MultipleUploadController extends Controller
14{
15 
16public function store(Request $request)
17{
18    if(!$request->hasFile('fileName')) {
19        return response()->json(['upload_file_not_found'], 400);
20    }
21 
22    $allowedfileExtension=['pdf','jpg','png'];
23    $files = $request->file('fileName'); 
24    $errors = [];
25 
26    foreach ($files as $file) {      
27 
28        $extension = $file->getClientOriginalExtension();
29 
30        $check = in_array($extension,$allowedfileExtension);
31 
32        if($check) {
33            foreach($request->fileName as $mediaFiles) {
34 
35                $path = $mediaFiles->store('public/images');
36                $name = $mediaFiles->getClientOriginalName();
37      
38                //store image file into directory and db
39                $save = new Image();
40                $save->title = $name;
41                $save->path = $path;
42                $save->save();
43            }
44        } else {
45            return response()->json(['invalid_file_format'], 422);
46        }
47 
48        return response()->json(['file_uploaded'], 200);
49 
50    }
51}
52 
53}
54