由于使用Laravel的未知错误而无法上传文件

时间:2019-02-15 15:03:13

标签: laravel laravel-5 laravel-5.2 laravel-5.5

  • Laravel版本:5.5

上传文件时出现问题。我在数组中有多个文件,我第一次在同一函数中上载这些文件两次,这是我第一次检查pdf文件版本,并且此代码块的文件尺寸很好,但是在第二个代码块中,我再次上载这些文件合并这些文件,这给了我这个错误“ 由于未知错误,文件“ A4.pdf”未上传”。当我删除第一个代码块然后第二个代码块开始工作时,我不知道自己在哪里弄错了,我进行了很多搜索,但没有找到答案。

此代码段用于检查pdf文件的版本和尺寸。

$paper_size = array();
$del_files = array();
foreach ($files as $file) {

        $filename = time().date('m-d-y').$file->getClientOriginalName();

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

        $version = $this->pdfVersion(public_path().'/uploads/check_pdf_files/'.$filename);

        if($version > 1.5)
        {
            File::delete('public/uploads/check_pdf_files/'.$filename);

            return Response::json(" Your PDF file version is greater than 1.4 which is not compatible with our system, Please make it lower version.", 400);
        }

        $get_paper_size = $this->get_pdf_dimensions('public/uploads/check_pdf_files/'.$filename);

        $paper_size[] = $get_paper_size;
        $del_files[] = $filename;
    }

    if(round($paper_size[0]['width']) != round($paper_size[1]['width']))
    {
        foreach ($del_files as $del) 
        {
            File::delete('public/uploads/check_pdf_files/'.$del);
        }

        return Response::json(" Your Files dimensions is not matching please try with same dimensions.", 400);
    }

此代码块用于合并文件。

         $new_pdf_file = array();

         foreach ($request->file as $merge_file) 
            {
                $newFile_name = time().$merge_file->getClientOriginalName();


                $merge_file->move('public/uploads/', $newFile_name);

                $new_pdf_file[] = $newFile_name;
            }
            dd($new_pdf_file);

            $pdf = new \LynX39\LaraPdfMerger\PdfManage;

            foreach($new_pdf_file as $new)
            {
                $pdf->addPDF('public/uploads/dummy_uploads/'.$new, 'all');
            }

            $temp_name = time().$request->merge_name;

            $pdf->merge('file',base_path(). '/public/uploads/' . Auth::user()->email . '/'.$temp_name.'.pdf', 'P');

            foreach($new_pdf_file as $delete_new)
            {
                File::delete('public/uploads/dummy_uploads/'.$delete_new);
            }

            $user = DB::table('user_pdf_files')->insert([
                'user_files' => $request->merge_name.'.pdf',
                'filename' => $temp_name.'.pdf',
                'type' => $request->type[0],
                'user_id'    => Auth::user()->id,
            ]);

            Session::flash('success', 'Files Merged Successfully');
            return Response::json('success', 200);`

1 个答案:

答案 0 :(得分:0)

您需要将文件保存到的标准路径。任何时候移动或复制文件时,请确保您使用public_path()并将相对路径作为参数。此功能将标准路径输出到公用文件夹。例如:

$merge_file->move(public_path('uploads'), $newFile_name);

这应该是第一个代码块相对于第二个代码块有效的原因。但是,这不是一个非常描述性的错误!