Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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上载和显示保存在PostgreSQL中的PDF文件_Php_Laravel_Postgresql_Pdf - Fatal编程技术网

Php 使用Laravel上载和显示保存在PostgreSQL中的PDF文件

Php 使用Laravel上载和显示保存在PostgreSQL中的PDF文件,php,laravel,postgresql,pdf,Php,Laravel,Postgresql,Pdf,我有一个项目,要求我在数据库中保存PDF文件(将文件保存在数据库中,而不是路径)。我正在使用PostgreSQL 10和Laravel 5.6。这是我的迁移文件: public function up() { Schema::create('uploads', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $tabl

我有一个项目,要求我在数据库中保存PDF文件(将文件保存在数据库中,而不是路径)。我正在使用PostgreSQL 10和Laravel 5.6。这是我的迁移文件:

public function up()
{
    Schema::create('uploads', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('mime');
        $table->binary('data'); // generate a bytea column after running migration
        $table->timestamps();
    });
}
Schema::create('uploads', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('mime');
        $table->text('data');
        $table->timestamps();
});
下面是我的控制器中上载PDF文件的功能:

public function upload(Request $req)
{
  $name = pg_escape_string($_FILES['fpdf']['name']);
  $type = pg_escape_string($_FILES['fpdf']['type']);
  $data = pg_escape_bytea(file_get_contents($_FILES['fpdf']['tmp_name']));

  DB::table('uploads')
  ->insert(['name' => $name, 'mime' => $type, 'data' => $data]);

  return "success upload";
}
public function upload()
{
    $name = $_FILES['fpdf']['name'];
    $mime = $_FILES['fpdf']['type'];
    $content = base64_encode(file_get_contents($_FILES['fpdf'['tmp_name']));

    $up = new Upload;

    $up->name = $name;
    $up->mime = $mime;
    $up->content = $content;

    $up->save();

    return redirect('upload');
}
public function showPDF($id)
{
    $pdf = Upload::find($id);

    $content = base64_decode($pdf->content);
    return response($content)->header('Content-Type', $pdf->mime); // thanks to Devon
}
已成功上载PDF文件:

但问题是,无法在浏览器中显示(下载)PDF文件。这是我的控制器中用于在浏览器中显示PDF文件的功能:

public function showPDF($id)
{
  $pdf = Upload::find($id);
  return view('upload/pdf', compact('pdf'));
}
查看文件:

<?php
    $content = pg_unescape_bytea(stream_get_contents($pdf->data));
    echo "<embed src='data:". $pdf->mime .";base64,".base64_encode($content)."' />";
?>

现在大多数浏览器都内置了PDF阅读器,通常没有理由使用embed,除非你真的想将PDF嵌入到其他内容中

为此,您应该能够从控制器返回响应,并为内容类型设置标题:

return response($content)->header('Content-Type', $pdf->mime);

在尝试了几种组合后,我找到了解决方案。以下是我的解决方案:

迁移文件:

public function up()
{
    Schema::create('uploads', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('mime');
        $table->binary('data'); // generate a bytea column after running migration
        $table->timestamps();
    });
}
Schema::create('uploads', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('mime');
        $table->text('data');
        $table->timestamps();
});
我在迁移文件中所做的是更改列数据的数据类型,以前我使用的是二进制,这里我使用的是文本

“我的控制器”中用于上载PDF文件的函数:

public function upload(Request $req)
{
  $name = pg_escape_string($_FILES['fpdf']['name']);
  $type = pg_escape_string($_FILES['fpdf']['type']);
  $data = pg_escape_bytea(file_get_contents($_FILES['fpdf']['tmp_name']));

  DB::table('uploads')
  ->insert(['name' => $name, 'mime' => $type, 'data' => $data]);

  return "success upload";
}
public function upload()
{
    $name = $_FILES['fpdf']['name'];
    $mime = $_FILES['fpdf']['type'];
    $content = base64_encode(file_get_contents($_FILES['fpdf'['tmp_name']));

    $up = new Upload;

    $up->name = $name;
    $up->mime = $mime;
    $up->content = $content;

    $up->save();

    return redirect('upload');
}
public function showPDF($id)
{
    $pdf = Upload::find($id);

    $content = base64_decode($pdf->content);
    return response($content)->header('Content-Type', $pdf->mime); // thanks to Devon
}
我使用base64\u encode()函数对PDF文件进行编码。下面是显示PDF文件的方法:

public function upload(Request $req)
{
  $name = pg_escape_string($_FILES['fpdf']['name']);
  $type = pg_escape_string($_FILES['fpdf']['type']);
  $data = pg_escape_bytea(file_get_contents($_FILES['fpdf']['tmp_name']));

  DB::table('uploads')
  ->insert(['name' => $name, 'mime' => $type, 'data' => $data]);

  return "success upload";
}
public function upload()
{
    $name = $_FILES['fpdf']['name'];
    $mime = $_FILES['fpdf']['type'];
    $content = base64_encode(file_get_contents($_FILES['fpdf'['tmp_name']));

    $up = new Upload;

    $up->name = $name;
    $up->mime = $mime;
    $up->content = $content;

    $up->save();

    return redirect('upload');
}
public function showPDF($id)
{
    $pdf = Upload::find($id);

    $content = base64_decode($pdf->content);
    return response($content)->header('Content-Type', $pdf->mime); // thanks to Devon
}
查看文件:

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
    <meta charset="utf-8">
    <title>Demo Upload</title>
</head>
<body>
    <h1>Form Upload PDF File</h1>

    <form action="upload" method="post" enctype="multipart/form-data">
        @csrf

        <br>
        <input type="file" name="fpdf">
        <br> <br>
        <input type="submit" value="Upload">
    </form>

    <p>&nbsp;</p>

    <h3>List of PDFs</h3>

    @foreach($pdfs as $row)
        <a href="show/{{ $row->id }}" target="_blank"> {{ $row->name }} </a>
        <br>
    @endforeach
</body>
</html>

演示上传
表格上传PDF文件
@csrf



PDF文件列表 @foreach($PDF作为$row)
@endforeach

这是我目前的解决方案。我知道这可能不是处理文件上传的最佳方式,但它确实有效。欢迎提出任何使代码更好、更安全的建议。

为什么要使用转义字符串函数?你真的想嵌入它还是让他们下载它?嗨,德文,我想做任何一个,但我更喜欢下载PDF文件。你真的不应该把pg_uuu函数和Laravel结合起来。无论如何,停止使用转义字符串函数,即使在Laravel之外,您也应该使用准备好的语句。我在showPDF函数中尝试了您的代码,但它给了我一个错误:响应内容必须是实现u toString()、给定“resource”的字符串或对象。我修改了showPDF函数,但现在加载PDF文档失败(以上问题的详细信息)。我遗漏了什么?您的第一条消息表明$content不是字符串。