【问题标题】:Displaying .pdf file in browser with PHP in Laravel 7 using RESTful Controller使用 RESTful 控制器在 Laravel 7 中使用 PHP 在浏览器中显示 .pdf 文件
【发布时间】:2020-08-13 01:52:12
【问题描述】:

我正在开发一个 laravel 项目,在项目内部我将 .pdf 文件存储到我的本地 phpmyadmin 并将其检索回来以显示在浏览器中。

我看到一些应用程序,但它们在将 .pdf 文件上传到数据库时将其保存在本地。然后用<img src=""> 显示变得很容易,但我不想使用它。

这些是我的路线;

Route::get('/users/{user}/posts', 'PostController@index');
Route::get('/posts/create', 'PostController@create');
Route::post('/posts', 'PostController@store');

和索引函数;

public function index(User $user, Post $posts)
    {
        $posts = DB::table('posts')
                        ->where('userId', '=', $user->id)
                        ->get();

        return view('post.index', compact('posts'));
    }

我可以上传和存储可以但无法在浏览器中显示的.pdf 文件。 所以我想在 PostController 中使用 index 函数获取记录(已经完成),并在来自 db 的 index.blade.php 文件中显示 .pdf 文件。 像这样:http://localhost/test.pdf 当我在浏览器中显示它时,我只能看到它的名称。如何读取从db 获取的文件?

感谢您的回答。

【问题讨论】:

  • 小心,您的PhpMyAdmin 是您的mysql/mariadb 数据库的Web 管理工具,与您的问题无关。您要存储完整文件还是仅存储数据库中文件的路径?比您要显示 pdf 文件还是 pdf 文件的图像?
  • 不不是文件路径。我想将完整的文件存储在数据库中,并且我想从数据库中获取所有文件,例如链接它们将是可单击的,当您单击它时,我想显示 .pdf 文件。这就像在浏览器中打开一个 .pdf 文件一样。向下滚动并阅读。

标签: php laravel pdf blob


【解决方案1】:

首先,在我看来,您应该将文件存储到storage system of laravel 而不是数据库中。

但是,如果您想使用数据库执行此操作,这里是一个输出文件的示例,该文件存储在数据库的 blob 字段中(例如在files 表的content 字段中)。

另一种不太漂亮的方法是将文件转换为base64 字符串并存储在文本字段see more here 中。

“db_files”表的架构

field     | type
----------|-------------
id        | BIGINT
name      | VARCHAR(255)
content   | BLOB
mime_type | VARCHAR(255)

路线

Route::get('/files/{id}', 'FileController@show')->name('file.show');

DbFile 模型

use Illuminate\Database\Eloquent\Model;

class DbFile extends Model {
    // ...
}

文件控制器

public function show($id) {
    $dbFile = DbFile::firstOrFail($id);

    // the $dbFile->mime_type should be 'application/pdf' for pdf files
    // the $dbFile->name should be in this schema 'document.pdf'
    return response($dbFile->content)
        ->withHeaders([
            'Content-type: ' . $dbFile->mime_type,
            'Content-Disposition: attachment; filename=' . $dbFile->name
        ]);
}

查看

<a href="{{ route('file.show', ['id' => 1]) }}" target="_blank">Show Document</a>

我现在无法测试代码。如果出现问题,请告诉我。

【讨论】:

  • 好的,我明白了。如果这些文件/图像在存储或公共文件夹中的数量会增加很多,例如 10.000 个文件/图像,会影响系统/网站的工作速度吗?
  • 如果你想存储大量数据,你应该检查你是否有足够的可用空间。对于性能方面,您还可以考虑云解决方案。对于您自己的存储,您应该考虑一个好的系统如何快速找到文件(例如,使用子文件夹结构)。在此处查看更多信息:stackoverflow.com/questions/2648664/…
【解决方案2】:

有一种方法可以通过更改文件的扩展名来解决这个问题 pdfpdfz 或任何其他扩展名并使用此代码

$realPath="path file with pdfz extension";

                 if(file_exists($realPath)){
                   $realPath=str_replace(".pdf",".pdfz",$realPath);
                $file =$realPath;
    $filename = $realPath;
    header('Content-type: application/pdf');
    header('Content-Disposition: inline; filename="' . $filename . '"');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . filesize($file));
    header('Accept-Ranges: bytes');
    @readfile($file);
    exit;
               }else{
    echo "$realPath:No File Exist";

               }

更改扩展名的目的是防止强制下载 by IDM

【讨论】:

  • 文件路径是内部路径吗?像 App\folder\file.pdfz。我从数据库中获取文件,当我在浏览器中显示它时,我只能看到它的名称。如何读取从 db 获取的文件?
猜你喜欢
  • 1970-01-01
  • 2017-05-28
  • 2023-03-15
  • 2014-08-03
  • 1970-01-01
  • 1970-01-01
  • 2023-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多