Laravel 与 postgresql,存储和获取二进制数据

问题描述 投票:0回答:3

如何使用 Laravel 从 postgres bytea 字段存储和获取数据? 我想更新二进制数据并下载它们。 文件类型有jpg、excel、txt等。 我可以做吗?

目前我要存储的代码。

    public function store_db( $file, $file_name, $user_id ) {

    $file_path = $file->getRealPath();

    $new_attachment = Attachment::create([
        'name' => $file_name,
        'mime' => $file->getClientMimeType(),
        'size' => $file->getClientSize(),
        'uploaded_data' => pg_escape_bytea(file_get_contents($file_path)),
        'created_by' => $user_id,
        'updated_by'=> $user_id,
        'created_at' => Carbon::now(),
        'updated_at' => Carbon::now()
    ]);

    return $new_attachment->id;

接下来获取和下载数据(jpg、excel等)

    public function get_attachment( $id ) {
    $file = $this->attachmentRepository->findWithoutFail($id);

    $dbh = DB::connection()->getPdo();

    $stmt = $dbh->prepare("SELECT name, mime, trim(trailing from encode(uploaded_data,'escape')) AS encode_data FROM attachments WHERE attachments.id = :atid");
    $stmt->bindParam(':atid', $id);
    $stmt->execute();
    $result = $stmt->fetch($dbh::FETCH_ASSOC);

    $name = $result['name'];
    $mime = $result['mime'];
    $headers = array(
        "Content-Type: {$mime}",
    );
    $fileData = $result['encode_data'];

    $ext = substr($name, strrpos($name, '.') + 1);
    file_put_contents($ext , pg_unescape_bytea($fileData));


    return response()->download($ext, $name, $headers);
php laravel postgresql download binary
3个回答
1
投票

您的部分代码对我自己使用 Laravel 7 很有用。这里我的解决方案对我来说 100% 有效。我还在等待它对你有帮助。抱歉,我的英语不是我的母语。

        $dbh = DB::connection()->getPdo();
        $query = "SELECT *, trim(trailing from encode(uploaded_data,'base64')) AS encode_data FROM esq_inversion.view_documento_proyecto WHERE id = :id";
        $stmt = $dbh->prepare($query);
        $stmt->bindParam(':id', $id);
        $data = $stmt->execute();
        // Convert to binary and send to the browser
        $stream = base64_decode($data['encode_data']);

        return response()->streamDownload(function () use ($stream){
            $file = fopen('php://output', 'w');
            fwrite($file, $stream);
            fclose($file);
        }, $fileName,[
            'Content-Type' => $mime,
            'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
            'Content-Disposition' => 'attachment;filename*=UTF-8\'\'' . $fileName.'";',
            'Expires' => '0',
            'Pragma' => 'public',
        ], 'attachment');

最诚挚的问候,米格


0
投票

对我来说,我从数据库恢复的所有文件都已损坏。然后我发现

pg_escape_bytea()
到处都返回,而不是一个撇号 -> 两个。 所以我在存储二进制数据时使用
str_replace
并解决了问题:

File::create([
 'report_id' => $report->id,
 'name' => $file->getClientOriginalName(),
 'mime' => $file->getClientMimeType(),
 'size' => $file->getSize(),
 'data' =>  str_replace("''", "'", pg_escape_bytea(file_get_contents($file_path))),
 'created_at' => Carbon::now(),
 'updated_at' => Carbon::now()
]);

然后是我下载文件的方法:

public function fileDownload(File $file)
    {
        file_put_contents($file->name , stream_get_contents($file->data));

        $headers = array(
            "Content-Type: {$file->mime}",
            "Content-Length: {$file->size}"
        );

        return response()->download($file->name, $file->name, $headers)->deleteFileAfterSend(true);
    }

0
投票

谢谢你,这对我很有帮助

© www.soinside.com 2019 - 2024. All rights reserved.