如何上传文件并以原始名称保存在存储文件夹中

How to upload file and save it with original name in storage folder

我想用原始文件保存上传的文件name.What我必须添加到此代码吗?

下面是我的代码

public function store(Request $request)
{
    if($request->hasFile('image'))
    {
        $file = $request->file('image');
        $originalname = $file->getClientOriginalName();
        $filename =$originalname;
        $file->move('public/', $filename);
    }

您可以使用 storeAs 方法,该方法接收路径、文件名和(可选)磁盘作为其参数:

public function store(Request $request)
{
    if($request->hasFile('image'))
    {
        $file = $request->file('image');
        $originalname = $file->getClientOriginalName();
        $path = $file->storeAs('public/', $originalname);
    }
}

试试这个:

if($request->hasFile('image'))
{
    $file = $request->file('image');
    $originalname = $file->getClientOriginalName();
    $img = Image::make($file->getRealPath());
    $img->stream();
    Storage::disk('local')->put('images/'.$originalname, $img, 'public');
}

使用原始文件名上传文件。

if ($request->hasFile('image')) {
    $file_path = $request->file('image')->storeAs('dir_name', request()->file('image')->getClientOriginalName(), 'optional_disk_name');
}

// pass the disk name as the third argument to the storeAs method: ex. local,s3,etc