我已经通过 post 方法将图像文件存储在管理员中,但是当尝试为 pdf/word 放置相同的代码时它不起作用

I have store the image file in admin through post method but when trying to put the same code for pdf/word its not working

我想在页面上上传 pdf 文件,但它无法像处理图片那样正常工作

我已经为 pdf/word 创建了一个列文件路径,然后在 postcontroller 中编写代码但没有工作

public函数存储(请求$request) {

    $this->validate($request, [


       'title' =>'required',
        'featured'=>'required|image',
        'content'=>'required',
        'category_id'=>'required'

    ]);

    $featured= $request->featured;
    $featured_new_name=time().$featured->getClientOriginalName();
    $featured->move('uploads/posts', $featured_new_name);

    $post = Post::create([

        'title'=>$request->title,
        'content'=>$request->content,
        'featured'=>'uploads/posts/'. $featured_new_name,
        'category_id'=>$request->category_id,
        'slug'=>str_slug($request->title)

当我尝试为 pdf/word 添加 "filepath column name in data base" 然后在 public 函数存储中使用时(请求 $request) {

    $this->validate($request, [


       'title' =>'required',
        'featured'=>'required|image',
        'content'=>'required',
        'category_id'=>'required',

        'file' => 'required',

    ]);

    $featured= $request->featured;
    $featured_new_name=time().$featured->getClientOriginalName();
    $featured->move('uploads/posts', $featured_new_name);

    $file=$request->file;
    $file=time().$file->getClientOriginalName();
    $extension = Input::file('file')->getClientOriginalExtension();

    $filename = rand(11111111, 99999999). '.' . $extension;
    $fullPath = $filename;
    $request->file('file')->move(base_path() . '/uploads/pdf/', $filename);



    $post = Post::create([

        'title'=>$request->title,
        'content'=>$request->content,
        'featured'=>'uploads/posts/'. $featured_new_name,
        'file'=>'uploads/pdf' .$filename,

        'category_id'=>$request->category_id,
        'slug'=>str_slug($request->title)


    ]);


  Session::flash('success', 'New Blog has been Published on Website for Particular Menu');

  return redirect()->back();

}

您的代码中存在问题,您将图像保存在基本路径中,而您想将其保存到 public 路径中,因此请使用

$request->file('file')->move(public_path() . '/uploads/pdf/', $filename);

而不是

  $request->file('file')->move(base_path() . '/uploads/pdf/', $filename);

要保存文件请更新您的代码 文件类型验证

'file' => 'required|mimes:pdf,doc|max:10000',

保存文件

  if($request->hasfile('file'))
    {
        $file = $request->file;
        $input = rand(11111111, 99999999).'.'.$file->getClientOriginalExtension();
        $destinationPath = public_path('/uploads/pdf/100');
        $file->move($destinationPath, $input);
   }