【问题标题】:Image store but not show proper path in laravel API图像存储但未在 laravel API 中显示正确路径
【发布时间】:2019-11-25 17:20:41
【问题描述】:

如何在数据库中存储实际路径以及如何在 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); 
    }

【问题讨论】:

  • 代码在数据库中存储数据在哪里?
  • 店内功能..你可以检查问题-@ShaileshLadumor
  • 您必须将您的图像上传到您的存储文件夹中,然后将文件名存储在您上传图像后检索的数据库中。在您的 API 中传递数据时,将 URL 链接附加到您的存储中并带有图像名称。您可以在此处找到更好的图像上传代码块。 stackoverflow.com/questions/58856971/…

标签: laravel laravel-5 laravel-4 postman laravel-api


【解决方案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);
    }

【讨论】:

    【解决方案2】:

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

                    $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)]]);

    【讨论】:

    • 对不起其实我不明白你的代码。 -@PHP 极客
    • 他已经存储了一个唯一的图像名称。 $fileName = time() . '.'.$file->clientExtension();这一行创建了带有扩展名的独特时间不是吗?
    • 是的 -@DilipHirapara
    • 我只了解我的代码,你能检查一下我的代码中的错误吗?-@PHP Geek
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多