我对 laravel 文件上传图片有疑问

i have problem with laravel fileupload image

谁能帮帮我,我想添加带图片的文章。图片已成功进入目录但在数据库中名称始终为D:\xampp\tmp\php......tmp.

我已将系统文件更改为public。

控制器

    public function store(Request $request)
    {
        //

        $validateData = $request->validate([
            'title' => 'required|max:255',
            'thumbnail' => 'image|file|max:8192',
            'slug' => 'required',
            'description' => 'required',
        ]);
        if ($request->file('thumbnail')) {
            $imageName = time().'.'.$request->file('thumbnail')->getClientOriginalExtension();
            $validatedData['thumbnail'] = $request->thumbnail->move(public_path('uploads/article/'), $imageName);
        }
             
        //dd($validateData['thumbnail']);
        Article::create($validateData);
        return redirect('/admin-article')->with('success', 'Data has been successfully added');
    }

试试这个

if ($request->file('thumbnail')) {
   $imageName = time().'.'.$request->file('thumbnail')->getClientOriginalExtension();
   $request->thumbnail->move(public_path('uploads/article/'), $imageName);
   $validatedData['thumbnail'] = url('uploads/article/'.$imageName);
}

它是 return 路径而不是 url 的原因是因为您使用的是 public_path 而不是 url()

控制器代码

public function store(Request $request)
{
    $validateData = $request->validate([
        'title'       => 'required|max:255',
        'thumbnail'   => 'image|file|max:8192',
        'slug'        => 'required',
        'description' => 'required',
    ]);

    // Check if request has file
    if($request->hasFile('thumbnail')){
        // Get File
        $file = $request->file('thumbnail'); 
        // Get File Extention
        $fileGetFileExtension = $file->getClientOriginalExtension(); 
        // Create customized file name
        $fileName = Str::random(20).'_'.date('d_m_Y_h_i_s').'.'.$fileGetFileExtension; 
        // Save File to your storage folder
        Storage::disk('public')->put('uploads/article/'.$fileName,File::get($file));
    }else{
        $fileName = null;
    }

    $validatedData['thumbnail'] = $fileName;

    Article::create($validateData);

    return redirect('/admin-article')->with('success', 'Data has been successfully added');
}

运行php artisan storage:link,如果没有创建

在 blade 中你可以这样得到你的文件

<a href="{{ url('storage/uploads/article/'.$actical->thumbnail) }}"></a>

希望对您有所帮助。 :D

感谢您的回答我的问题已经用这样的代码解决了,但只是存储在数据库中的文件名没有目录名。

public function store(Request $request)
{
    $validateData = $request->validate([
        'title' => 'required|max:255',
        'thumbnail' => 'image|file|max:8192',
        'slug' => 'required',
        'description' => 'required',
    ]);
    $imageName = time().'.'.$request->thumbnail->getClientOriginalExtension();
    $request->thumbnail->move(public_path('articles/'), $imageName);
    $Article = new Article;
    $Article->title = $validateData['title'];
    $Article->thumbnail =  $imageName;
    $Article->slug = $validateData['slug'];
    $Article->description  = $validateData['description'];
    $Article->save();

    return redirect('/admin-article')->with('success', 'Data has been successfully added');
}