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

时间:2019-04-10 05:35:44

标签: laravel-5 laravel-5.2 laravel-5.1

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

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

公共功能存储(请求$ 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添加“数据库中的文件路径列名称”,然后在公共函数存储中使用时(请求$ 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();

}

1 个答案:

答案 0 :(得分:0)

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

$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);
   }