Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用Laravel Framwork保存和显示PDF、Excel和Word文件?_Php_Laravel_File Upload - Fatal编程技术网

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()无法访问为什么?