php word文档只读代码

发布于 2024-12-04 04:34:04 字数 821 浏览 2 评论 0原文

我有一个php下载脚本,允许用户在下载后下载一个word文档文件,该文件可供用户编辑。我想将文件设置为只读以供用户下载,

我的下载代码是:

//php codethanx

// set headers`enter code here`
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: $mtype");
header("Content-Disposition: attachment; filename=\"$asfname\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);

// download
// @readfile($file_path);

$file = @fopen($file_path,"rb");
if ($file) {
  while(!feof($file)) {
    print(fread($file, 1024*8));
    flush();
    if (connection_status()!=0) {
      @fclose($file);
      die();
    }
  }
  @fclose($file);
}

in advance 问候

I have a php download script that allow user to download a word document file after downloading it is editable to user..i want to make file readonly to user

my code for download is:

//php code

// set headers`enter code here`
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: $mtype");
header("Content-Disposition: attachment; filename=\"$asfname\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);

// download
// @readfile($file_path);

$file = @fopen($file_path,"rb");
if ($file) {
  while(!feof($file)) {
    print(fread($file, 1024*8));
    flush();
    if (connection_status()!=0) {
      @fclose($file);
      die();
    }
  }
  @fclose($file);
}

thanx in advance
Regards

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

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

发布评论

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

评论(4

烟凡古楼 2024-12-11 04:34:04

你不能。文件属性不会随下载一起发送。用户下载该文件并拥有它。

您可以使用密码保护该文档,但为此您必须自己对其进行编辑。这是 PHP 很难完成的事情,尽管您可以在 Windows 上使用 PHP 的 Word 的 COM 接口...但这不是您想要走的路。

You can't. File attributes aren't sent with the download. The user downloads the file and owns it.

You can password-protect the document, but for that you have to edit it yourself. It is something that can hardly be done by PHP, although you could use the COM interface of Word from PHP on Windows... But that's not a path you want to go.

私野 2024-12-11 04:34:04

如果您想要只读文档,为什么不使用 pdf 呢?

If you want a readonly document, why not use a pdf instead?

清风夜微凉 2024-12-11 04:34:04

以下是使用 PHPWord 库制作受密码保护的不可编辑 Word 文件的方法:

https://github.com/PHPOffice/ PHPWord

<?php 
require_once("vendor/autoload.php"); 

// Input file
$name = basename(__FILE__, '.php');
$name="sample";
$source = __DIR__ . "/{$name}.docx";

$phpWord = \PhpOffice\PhpWord\IOFactory::load($source);

// Add Password Protection For editing 
$documentProtection = $phpWord->getSettings()->getDocumentProtection();
$documentProtection->setEditing(PhpOffice\PhpWord\SimpleType\DocProtect::READ_ONLY);
$documentProtection->setPassword('123456');

// Write the output file

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');

Here is method to make password protected non editable word file using the PHPWord library:

https://github.com/PHPOffice/PHPWord

<?php 
require_once("vendor/autoload.php"); 

// Input file
$name = basename(__FILE__, '.php');
$name="sample";
$source = __DIR__ . "/{$name}.docx";

$phpWord = \PhpOffice\PhpWord\IOFactory::load($source);

// Add Password Protection For editing 
$documentProtection = $phpWord->getSettings()->getDocumentProtection();
$documentProtection->setEditing(PhpOffice\PhpWord\SimpleType\DocProtect::READ_ONLY);
$documentProtection->setPassword('123456');

// Write the output file

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');
橘虞初梦 2024-12-11 04:34:04

更新::我的解决方法和新答案......无意编辑保护。

快速:这是我发现的从 PHP 脚本“接近”实现防篡改 .docx 文件的最简单也是第一种方法。我目前无法使用 PHP 实现动态添加保护。

在我创建动态 .docx 文件的早期阶段,我了解到编辑文件更容易,如 此处

请注意,在替换文档中的文本时,word 的拼写检查功能会通过将相关单词 (my_dynamic_file_title) 的部分内容包装在拼写检查和语法标记中,从而扭曲纯文本,将其视为拼写错误的单词。您可能还会发现,除非您关闭所有拼写检查和语法功能,否则它将无法工作,如此处。我不得不这么做。

请注意,完成后我必须复制并粘贴文本(大部分带有格式)以完全删除拼写检查和语法标签。我在模板 .docx 中使用的占位符我必须从 word 外部复制并粘贴它们而无需格式化,或者我只是重新输入它们。

现在..如果在word中(我的是Office 2019),在您的文档模板上转到:

文件->保护文档->限制编辑

选择您想要的选项并用密码保护它。上传您的模板并按照您的意愿使用编辑脚本。它的工作原理与受保护之前相同。

此方法阻止 word 允许在没有密码的情况下编辑文件,而不是提取文件、更改 document.xml 文件并通过简单地从 .docx 重命名文件扩展名将其转换回 docx 文件的人 (Dev)。到 .zip 并返回。

::我的旧答案:: 所以是的,有人可以做到这一点,但是......

在我自己研究并简要比较本机受保护(或我认为是)的文档之后,我可以看到这是可能的,并且以前已经做过IE..Szekelygobe 的答案提到了执行此操作的软件(或者有一个补丁可以执行此操作),如 github 这里

我必须说,为我自己做这件事似乎需要太多,而且由于缺乏现成的文档,这对我来说还不值得。如果您想沿着这条路走下去,请将 1 .docx 复制一份并保护其中一个。将它们重命名为 .zip 文件并在您最喜欢的代码编辑器中进行比较。

乍一看,最显着的区别是在名为“settings.xml”的文件中,您将看到标签

我还看到许多其他更改。其他标签不一样,这让我相信至少不仅仅是创建/生成这个标签。涉及加密,需要研究特定标签属性的属性以及整个内容和内容。每个如何......

就像我说的,虽然对我来说试图理解自动取款机实在是太难了,但我希望我能够指出一条路。

注意::我本来想发表评论的,但这对于评论来说已经太多了。

关于所有这些的最后一点:我真的很想从 PHP 中完成这一切,但目前它太多了。我还想让它完全防篡改,但这是不可能的。你可以接近,但要花费时间和精力才能让这一切变得可伪造。 Word 具有数字签名和 IRM(信息权限管理),我相信这也有助于显示真实性。

UPDATE:: My workaround and new answer... Unintentional Editing Protection.

Quickly: This is the easiest and first way I have found to get "close" to achieving tamper proof .docx file from PHP script. I could not achieve adding the protection on the fly with PHP at this time.

So early in my venture to create dynamic .docx files I learned that it was easier to edit files as seen here.

Please note that when replacing text in the document, word's spellcheck feature distorts plain text that it sees as misspelled words by wrapping parts of that word in question (my_dynamic_file_title) in spellcheck and grammar tags. You will probably also find that it will not work unless you turn off all the spellcheck and grammar features as seen here. I had to.

Please note after I did I had to copy and paste the text (with formatting mostly) to fully remove the spellcheck and grammar tags. My place holders that I use in my template .docx I had to copy from outside of word and paste them without formatting or I just retyped them.

Now.. If, in word (Mine is Office 2019), on your document template you go to:

file-> Protect Document -> Restrict Editing

Choose the options that you want and password protect it. Upload your template and use the editing script as you would. It works the same as before it was protected.

This method stops word from allowing the editing of the file without a password, not a person(Dev) that extracts the files, changes the document.xml file, and converts it back to a docx file by simply renaming the file extensions from .docx to .zip and back.

::My old answer:: So yes someone can do it but...

After looking into it myself and briefly comparing natively protected (or what I believe to be) documents I can see it is possible as well as it has been done before IE.. Szekelygobe's answer mentions the software that does it (or there is a patch to do it) as seen in github here.

I must say it seems to require too much to do this for myself and with the lack of documentation readily available it just isn't worth it for me yet. If you would like to go down the path, take 1 .docx make a copy of it and protect one of them. Rename them to .zip files and compare in your favorite code editor.

The most notable difference at a brief glance is in the file called "settings.xml" you will see the tag <w:documentProtection...

I also see a lot of other changes. The other tags are not the same which leads me to believe there is at least a little more than just creating/generating this tag. There is encryption involved and it would require research into the properties of the specific tag properties and the whole what & how of each...

Like I said though it is just too much for me to try to understand a.t.m. but I hope I was able to point to a path.

NOTE::I would have commented but this became too much for a comment.

Last note on all of it:: I really wanted to do it all from PHP but it is just too much at the moment. I was also looking to make it completely tamper proof and it is just not possible. You can get close but it costs the time and effort just for it all to be forgable. Word has Digital Signatures and IRM (Information Rights Management) which I believe is to also help show authenticity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文