如何使用 php 保存 rtf 文件?

发布于 2024-11-16 22:24:03 字数 275 浏览 3 评论 0原文

$rtf=<<<RTF
    {\rtf1
    \b [TITLE] \b0\par
    \b [MESSAGE] \b0\par
    }
RTF;

$rtf = str_replace('[TITLE]',$valueTitle,$rtf);
$rtf = str_replace('[MESSAGE]',$valueMessage,$rtf);

当我像上面那样在 php 中编辑完 rtf 文件后,如何使用新文件名将其保存在特定位置以供人们下载?

$rtf=<<<RTF
    {\rtf1
    \b [TITLE] \b0\par
    \b [MESSAGE] \b0\par
    }
RTF;

$rtf = str_replace('[TITLE]',$valueTitle,$rtf);
$rtf = str_replace('[MESSAGE]',$valueMessage,$rtf);

After I have finished editing a rtf file in php like above, how do i save it with a new file name in a specific location for people to download?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞清仓 2024-11-23 22:24:03

您可以使用 file_put_contents() 保存它,并只需提供下载链接和适当的标头即可。

// $rtf holds your complete file data
file_put_contents('/path/to/outfile.rtf', $rtf);

然后您可以简单地给他们直接下载,或者使用正确的标头来提供它,如果您需要额外的处理或访问保护或其他东西,这很有用......

header("Content-type: application/rtf");
echo file_get_contents("/path/to/outfile.rtf");
exit();

You can save it with file_put_contents() and simply offer the link to download, with the appropriate headers.

// $rtf holds your complete file data
file_put_contents('/path/to/outfile.rtf', $rtf);

Then you can either simply give them the direct download, or serve it with the right headers like this, useful if you need additional processing or access protection or something...

header("Content-type: application/rtf");
echo file_get_contents("/path/to/outfile.rtf");
exit();
[旋木] 2024-11-23 22:24:03

找到我已经完成此操作的旧文件,所以这就是我现在正在使用的确切工作代码。

$rtf= file_get_contents('result.rtf');

$rtf = str_replace('[nickname]', $nickname, $rtf);
$rtf = str_replace('[secretWord]', $secretWord, $rtf);
$rtf = str_replace('[hoursInADay]', $hoursInADay, $rtf);
$rtf = str_replace('[hobby]', $hobby, $rtf);
$rtf = str_replace('[hobbyYears]', $hobbyYears, $rtf);
$rtf = str_replace('[hobbyAlphabet]', $hobbyAlphabet, $rtf);
$rtf = str_replace('[jokeFact]', $jokeFact, $rtf);

header('Content-type: application/msword');
header('Content-Disposition: attachment; filename=survey_result.rtf');
echo $rtf;
die();

found my old file where I had already done this, so this is the exact working code that I am using right now.

$rtf= file_get_contents('result.rtf');

$rtf = str_replace('[nickname]', $nickname, $rtf);
$rtf = str_replace('[secretWord]', $secretWord, $rtf);
$rtf = str_replace('[hoursInADay]', $hoursInADay, $rtf);
$rtf = str_replace('[hobby]', $hobby, $rtf);
$rtf = str_replace('[hobbyYears]', $hobbyYears, $rtf);
$rtf = str_replace('[hobbyAlphabet]', $hobbyAlphabet, $rtf);
$rtf = str_replace('[jokeFact]', $jokeFact, $rtf);

header('Content-type: application/msword');
header('Content-Disposition: attachment; filename=survey_result.rtf');
echo $rtf;
die();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文