如何在没有路径文件的情况下获取 Laravel 中的文件名

How to get file name in Laravel without a path file

我正在尝试将图像文件上传到数据库。但是,当它发布时,它显示了图像文件的完整路径(我使用 getClientOriginalName() 来获取文件名)。

DSController.php

public function createDS2(Request $request)
    {
        //dd($request->all());

        $imgName1 = $request->file('opsi_a')->getClientOriginalName() . '-' . time() . '.' . $request->file('opsi_a')->extension();
        $imgName2 = $request->file('opsi_b')->getClientOriginalName() . '-' . time() . '.' . $request->file('opsi_b')->extension();
        $imgName3 = $request->file('opsi_c')->getClientOriginalName() . '-' . time() . '.' . $request->file('opsi_c')->extension();
        $imgName4 = $request->file('opsi_d')->getClientOriginalName() . '-' . time() . '.' . $request->file('opsi_d')->extension();

        $data = new DS2;
        $data->pertanyaan = $request->input('pertanyaan');
        $data->opsi_a = $request->file('opsi_a')->move(public_path('image'), $imgName1);
        $data->opsi_d = $request->file('opsi_b')->move(public_path('image'), $imgName2);
        $data->opsi_b = $request->file('opsi_c')->move(public_path('image'), $imgName3);
        $data->opsi_c = $request->file('opsi_d')->move(public_path('image'), $imgName4);
        $data->kunci = $request->input('kunci');
        $data->save();
        return redirect('ds2');
    }

数据库中的图像文件将显示如下内容: C:\xampp\htdocs\admin_kuis\public\image\hiu_press.png-1614165665.png

如何才能将 hiu_press.png-1614165665.png 保存到我的数据库中?

将您的文件完整路径存储在 $stored_file 变量中,然后使用这个:

$data->opsi_d = $request->file('opsi_b')->move(public_path('image'), pathinfo($imgName2)['basename']);

另外,您可以使用:

$file = Input::file('image');
$fileName = $file->move(public_path('image'), $imgName1->getOriginalFileName());
$fileName->image = $fileName ->getRealPath();

Use This Code:

if ($request->hasFile('file_name')) {
    $file_name = $request->file('file_name');
    $file_name = time() . $file_name->getClientOriginalName();
}

与@zia-yamin 相同,但语法不同。

$imgName1 = $request->file('opsi_a')->getClientOriginalName() . '-' . time() . '.' . $request->file('opsi_a')->extension();
$imgName2 = $request->file('opsi_b')->getClientOriginalName() . '-' . time() . '.' . $request->file('opsi_b')->extension();
$imgName3 = $request->file('opsi_c')->getClientOriginalName() . '-' . time() . '.' . $request->file('opsi_c')->extension();
$imgName4 = $request->file('opsi_d')->getClientOriginalName() . '-' . time() . '.' . $request->file('opsi_d')->extension();

$request->file('opsi_a')->move(public_path('image'), $imgName1);
$request->file('opsi_b')->move(public_path('image'), $imgName2);
$request->file('opsi_c')->move(public_path('image'), $imgName3);
$request->file('opsi_d')->move(public_path('image'), $imgName4);

$data = new DS2;
$data->pertanyaan = $request->input('pertanyaan');
$data->opsi_a = pathinfo($imgName1, PATHINFO_BASENAME);
$data->opsi_b = pathinfo($imgName2, PATHINFO_BASENAME);
$data->opsi_c = pathinfo($imgName3, PATHINFO_BASENAME);
$data->opsi_d = pathinfo($imgName4, PATHINFO_BASENAME);
$data->kunci = $request->input('kunci');
$data->save();
return redirect('ds2');