php - 我想将图像上传到 laravel 中的公共(public)文件夹和数据库,但我遇到了一些问题

标签 php laravel

我想将图像上传到laravel中的公共(public)文件夹和数据库,但我遇到了一些问题,图像正在上传到公共(public)文件夹,但图像没有保存到数据库中。谁能帮我告诉我哪里出了问题?这是我的代码。

Controller

public function clientaction(Request $request)
   {


        $validation = validator::make($request->all(),[
          'select_file' =>'required|image|mimes:jpeg,png,jpg,gif|max:2048'
        ]);
        if($validation->passes())
        {
         $image = $request->file('select_file');
         $new_name = rand() . '.' . $image->getClientOriginalExtension();
         $image->move(public_path('images'), $new_name);
         return response()->json([
          'message'   => 'Image Upload Successfully',
          'class_name'  => 'alert-success'
         ]);


        }
        else
         {
          return response()->json([
          'message'   => $validation->errors()->all(),
         'class_name'  => 'alert-danger'
      ]);
     }

     DB::table('logo-clients')->insert([

      'select_file'=> $new_name
  ]);
   }

路线

Route::get('DashBoard','AdminController@DashBoard');


Route::get('Clients','AdminController@clients');
Route::post('/ajax_upload/action','AdminController@clientaction')->name('ajaxupload.action');

最佳答案

因为你已经返回了值,这意味着之后的操作是无用的

所以你只需要移动它

DB::table('logo-clients')->insert([

  'select_file'=> $new_name
]);

在这部分下面

$image->move(public_path('images'), $new_name);
//move it into here

关于php - 我想将图像上传到 laravel 中的公共(public)文件夹和数据库,但我遇到了一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58668304/

相关文章:

PHP:shell_exec 既未定义也未禁用

laravel - 如何在tinymce中忽略html标签| Vuejs 和 Laravel

php - 将 mysql 时间戳转换为来自 Code igniter 的实际时间 mm dd yyyy hh mm

php - 非常大的 xml 文件到 mysql

php - 在 Laravel 中合并两个 Eloquent 集合

laravel - 代客 502 网关错误

php - Laravel 5.2 pdf mime 类型验证问题

php - Laravel Eloquent(where 子句)

php - 在主页上显示获得足够点赞的帖子

php - MySql复杂查询统计每个人的订单