如何使用 Laravel 5.1 在表中保存上传的文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33150998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:30:39  来源:igfitidea点击:

How to save uploaded file name in table using Laravel 5.1

phpmysqllaravelfile-uploadlaravel-5.1

提问by dollar

I need help in saving uploaded file name in database table using laravel 5.1.

我需要帮助使用 laravel 5.1 在数据库表中保存上传的文件名。

My Controller code for saving Image details

用于保存图像详细信息的我的控制器代码

public function store(Request $request)
{
   if($request->hasFile('img_filename'))
   {
       $destinationPath="offerimages";
       $file = $request->file('img_filename');
       $filename=$file->getClientOriginalName();
       $request->file('img_filename')->move($destinationPath,$filename);

   }

    $input=$request->all();
    Offer_image::create($input);
    return redirect('offerimage');
}

My view code for accepting image

我的接受图像的查看代码

{!! Form::open(array('route'=>'offerimage.store','role'=>'form','files'=>true)) !!}
              <div class="box-body">

                <div class="form-group">
                  {!! Form::label('img_name','Name') !!}
                  {!! Form::text('img_name', $value = null, $attributes = array('class'=>'form-control','id'=>'img_name','required')) !!}
                </div>

                <div class="form-group">
                  {!! Form::label('img_description','Description') !!}
                  {!! Form::textarea('img_description', $value = null, $attributes = array('class'=>'form-control','id'=>'img_description','required')) !!}
                </div>

                <div class="form-group">
                  {!! Form::label('img_filename','Upload Image') !!}
                  {!! Form::file('img_filename') !!}
                </div>



                {!! Form::hidden('status',$value='active') !!}

              </div><!-- /.box-body -->

              <div class="box-footer">
                {!! Form::submit('Submit',$attributes=array('class'=>'btn btn-primary')) !!}
              </div>
              {!! Form::close() !!}

This controller code to store image working properly, but where i am trying to save image file name to table , this code is storing filepath to database table.

这个用于存储图像的控制器代码工作正常,但是在我尝试将图像文件名保存到表的地方,此代码将文件路径存储到数据库表。

As I am using direct create() method to store the request object in table, I don't know how do I store file name instead of path.

由于我使用直接 create() 方法将请求对象存储在表中,因此我不知道如何存储文件名而不是路径。

Check this Image for table data

检查此图像以获取表格数据

回答by darthsoup

The Problem is that your Request Data hasn't changed while you uploaded the picture. So img_filenamestill contains tmpdata.

问题是您上传图片时您的请求数据没有改变。所以img_filename仍然包含tmpdata。

You can try this:

你可以试试这个:

$input = $request->all();
$input['img_filename'] = $filename;

回答by fico7489

Code that works for me :

对我有用的代码:

$updir = 'images/';
$img_name = 'image.jpeg';
Request::file('img_filename')->move($updir, $img_name);