PHP:在Google云存储中保存“动态文本或pdf内容"吗? [英] PHP: Save 'dynamic text or pdf content' in google cloud storage?

本文介绍了PHP:在Google云存储中保存“动态文本或pdf内容"吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行时:php

GCS文件上传过程:

$storage = new StorageClient();
$file = fopen($source, 'r');
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload($file, [
  'name' => $objectName
]);

现在,我想使用动态文本/pdf创建对象,而不是使用$source路径文件.

Now i want to create object with dynamic text/pdf instead of using $source path file.

例如:

$storage = new StorageClient();
$objectName = "newfile.txt";
$content = "This is the dynamic content";
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload($content, [
    'name' => 'textDocs/'.$objectName
]);

因此它将创建一个新对象,其内容为"newfile.txt",内容为这是动态内容".

so it'll create new object as "newfile.txt" with content "This is the dynamic content".

是否有任何云库或函数来处理此问题?
预先感谢

is there any cloud libraries or functions to process this?
thanks in advance

推荐答案

对于在Google云存储中动态创建文本文档(带有动态内容),下面的代码将非常有效.

For Dynamic creation of text document (with-dynamic-content) in Google-cloud-storage, below code will be work perfectly.

$storage = new StorageClient();
$objectName = "newfile.txt";                  // name of object/text-file
$content = "This is the dynamic content";     // assign static/dynamic data to $content variable
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload($content, [
    'name' => 'textDocs/'.$objectName
]);

用于在Google云存储中动态创建pdf文档(具有动态内容).

For Dynamic creation of pdf document (with-dynamic-content) in Google-cloud-storage.

$name = "samplePDF.pdf";
$p = new PDFTable("L");
$p->AddPage();
$p->setfont('helvetica','',12);
$p->htmltable($html);
$pdfData = $p->output("",'S'); // returns pdf as string
$filename = "pdfDocs/".$name;
/** 
 * file uploading into google cloud storage : start
 */
$storage->bucket($bucketName, true)->upload(
    $pdfData,
    [
    'name' => $filename,
    'predefinedAcl' => 'publicRead'
    ]
);
/** 
 * file uploading into google cloud storage : end
 */
$p->output($name,"D"); // D->downloads file in our system.

这篇关于PHP:在Google云存储中保存“动态文本或pdf内容"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆