Php 如何使用Laravel Framwork保存和显示PDF、Excel和Word文件?
当我保存时,我会得到这个错误 未定义变量:文件 我真的不知道如何存储文件,但我在谷歌上查看了一些博客这是我的存储功能:Php 如何使用Laravel Framwork保存和显示PDF、Excel和Word文件?,php,laravel,file-upload,Php,Laravel,File Upload,当我保存时,我会得到这个错误 未定义变量:文件 我真的不知道如何存储文件,但我在谷歌上查看了一些博客这是我的存储功能: public function store(Request $request) { $this->validate($request, $this->rules); if($request->hasFile('file')){ $file = $request->file('file');
public function store(Request $request)
{
$this->validate($request, $this->rules);
if($request->hasFile('file')){
$file = $request->file('file');
$path = $file->getRealPath();
$files = file_get_contents($path);
}
$input = $request->all();
$charge = $request->get('charge');
$date = $request->get('date');
$job = $request->get('job_id');
$id = \Auth::id();
$current_id = DB::table('charges')->max('invoice_no');
$data = array();
foreach ($charge as $key=>$value ){
$data[] = [
'date' => $date,
'charge'=>$value,
'job_id'=>$job,
'user_id'=>$id,
'amount'=>$input['amount'][$key],
'category'=>$input['category'][$key],
'file'=>$files[$key], // here i will get error
'invoice_no' => $current_id + 1,
];
}
我正在创建我们公司的应用程序,所以我们将托管云。我很难将这些文件存储在我的数据库或磁盘中,但我们必须访问这些文件的任何地方。在我的显示视图中,我想用名称显示这个文件,当我点击这个名称时,我想显示那个特定的文档,这怎么可能呢
编辑
这是我的表格
{!! Form::model(new App\Charge, ['route' => ['charges.store'],'class'=>'form-horizontal','role'=>'form']) !!}
<div class="form-group">
{!! Form::label('date', 'DATE :',['class'=>'col-md-2 control-label']) !!}
<div class="col-md-7">
{!! Form::date('date',\Carbon\Carbon::now(),null,['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('job_id', 'JOB ID :',['class'=>'col-md-2 control-label']) !!}
<div class="col-md-7">
{!! Form::select('job_id',$job,null,['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('charges', 'CHARGES :',['class'=>'col-md-2 control-label']) !!}
<div class="col-md-10">
<div class="input_fields_wrap">
<button class="add_field_button" value="">Add More Charges</button>
<div>
<input type="text" name="charge[]">
<input type="text" name="category[]">
<input type="text" name="amount[]">
<input type="file" name="file[]">
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-2 col-md-offset-8">
<submitfiled>
{!! Form::submit(['class'=> 'btn btn-primary form-control']) !!}
</submitfiled>
</div>
</div>
{!! Form::close() !!}
{!!表单::模型(新应用程序\Charge、['route'=>['charges.store']、'class'=>'Form-horizontal'、'role'=>'Form'])
{!!Form::label('date','date:',['class'=>'col-md-2控件标签'])
{!!Form::date('date',\Carbon\Carbon::now(),null,['class'=>'Form control'])!!}
{!!Form::label('job_id','job id:',['class'=>'col-md-2 control label'])
{!!Form::select('job_id',$job,null,['class'=>'Form control'])
{!!Form::label('charges','charges:',['class'=>'col-md-2控制标签'])
加价
{!!表单::提交(['class'=>'btn btn主表单控件'])
{!!Form::close()!!}
您正在尝试访问$files
变量,该变量仅在上载文件时设置:
if($request->hasFile('file')){
$file = $request->file('file');
$path = $file->getRealPath();
$files = file_get_contents($path);
}
所以你可能没有上传文件。检查您的表单是否有enctype=“多部分/表单数据”
。通过在第2行检查文件,确保您正在上载文件:
dd($request->file('file'));
假设文件上传成功,您使用file\u get\u contents()
将整个文件读入字符串中,并将其分配给$files
但在代码的后面,您假定$files
是一个数组
:
'file'=>$files[$key], // here i will get error
试着调试变量,看看它们实际上是什么
另外,我会将文件复制到上载目录,并只保存此文件的路径,而不是将整个文件保存为字符串。我看到您在上载时遇到问题,但您已经做到了,对吗
在存储它之后,您将拥有一个带有参数的路由,用于流式传输或下载相关文件
也检查
您将找到一个很好的方法可以通过添加dd($files)进行调试代码>在第7行之后并更新您的问题?是的,与我使用$files时的错误相同,我在该行收到错误未定义变量:files请提供用于启动请求的表单的HTML标记。我刚刚尝试了dd($request->file('file')代码>它的空输出为什么?因为没有上传文件。您的
是否有enctype=“多部分/表单数据”
标签?您表单中输入的文件名是否为file
?我在表单中添加了encrypt我检查了dd(),现在我要退出该文件deatils但getRealPath();,getClientOriginalName()无法访问为什么?