1

如何在数据库中存储实际路径以及如何在 API 中获取图像?这是邮递员 API 的输出在此处输入图像描述

这是显示图像存储路径的数据库,但它是错误的..

在此处输入图像描述

 public function store(Request $request)
    {
        
        $event = $request->all(); 
           if ($request->hasFile('file')) {
$destinationPath = public_path().'/public/img/';
$file = $request->file;
$fileName = time() . '.'.$file->clientExtension();
$file->move($destinationPath, $fileName);
$input['e_image'] = $fileName;
}

            // $return['success'] = true,
            // $return['data'] = $event,
            // $return['msg'] = "this is message ";
             $success['status'] = true;
             $success['data'] = [$event];
             $success['message'] ="Event created successfully!";
             // $success['event'] = $event;
             return response()->json($success); 
    }

4

2 回答 2

1

$file = new YOURMODELNAME(); 请在这行代码中输入您的型号名称

public function store(Request $request)
     {
        $input = $request->all();
        $rules = array(
            'e_image' => 'required|mimes:jpeg,png,jpg,doc,docx,pdf,mp4,mov,ogg,qt',
       'e_group' => required,
       'e_invite'=>required,
        );
        $validator = Validator::make($input, $rules);
        if ($validator->fails()) {
            $arr = array("status" => 400, "message" => $validator->errors()->first(), "data" => array());
        } else {
            try {
                $file = $request->file('e_image');
                $input['e_image'] = time() . '.' . $file->getClientOriginalExtension();
                $destinationPath = public_path('/img/');
                $file->move($destinationPath, $input['e_image']);




                $file = new YOURMODELNAME();





                $file->e_image = $input['e_image'];
                $file->e_group = $input['e_group'];
                $file->e_invite = $input['e_invite'];
                $file->save();
                $file->e_image = url('public/img/' . $file->e_image);


                $arr = array("status" => 200, "message" => "file upload Successfully", "data" => $file);
            } catch (\Exception $ex) {
                if (isset($ex->errorInfo[2])) {
                    $msg = $ex->errorInfo[2];
                } else {
                    $msg = $ex->getMessage();
                }
                $arr = array("status" => 400, "message" => $msg, "data" => array());
            }
        }
        return \Response::json($arr);
    }
于 2019-11-25T09:20:41.163 回答
0

请尝试这个并将图像存储在具有唯一图像名称的正确项目目录中。

                $img = $request->profile_image;
                $old_path = public_path() . "/public/img/";
                $image_parts = explode(";base64,", $img);
                $image_type_aux = explode("image/", $image_parts[0]);
                $image_type = $image_type_aux[1];
                $image_base64 = base64_decode($image_parts[1]);
                $filename = uniqid() . '.png';
                $file = $old_path . $filename;
                file_put_contents($file, $image_base64);
                
                if (file_exists(public_path('/public/img/' . $filename))) {
                   //move image
                   $new_path = public_path() . "/public/img/newimages/";
                   if (!is_dir($new_path)) {
                       mkdir($new_path, 0777, TRUE);
                   }
                   $move = File::move($old_path . $filename, $new_path . $filename);
               }
               
               //upload image at database
               $modalObj = new table();
               $modalObj->updateById(\Session::get('table')->id, [
                   'profile_photo' => $filename,
               ]);
               return response()->json(['code' => '1', 'message' => 'Image Uploaded successfully!!!', 'data' => ['imageName' => url('/public/img/newimages/' . $filename)]]);

于 2019-11-25T05:18:59.367 回答