code for php word document read only

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

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.

code for php word document read only, 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� 1 thought on “ Read the content of a word document with PHP. Kundan Singh on April 21, 2016 at 8:19 am said: by this we can read only .docx file , but i want read .doc , .pdf , .txt files also.

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

Open a word processing document for read-only access , In these instances, you want to open the document for read-only access. OpenXML Words. The below sample code shows how you can add some text and attempt� Hi, I want to read and display MS word document with out changing its formates (like paragraph, color, bold, etc). I already try the following code.

You can use PHPWord to generate the document and make-it read only. https://phpword.readthedocs.io/en/latest/index.html

Word 16 document is opening in locked mode, Read the message: Hide Copy Code. READ ONLY : This Document is Lock for Editing by another user. That means just what it says: the file is� real Word documents. If you need to produce "real" Word documents you need a Windows-based web server and COM automation. I highly recommend Joel's article on this subject. fake HTTP headers for tricking Word into opening raw HTML. A rather common (but unreliable) alternative is:

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.

Write and edit source code - Help, When you work with code, PhpStorm ensures that your work is From the main menu, select File | Make File Read-only, or File | Make text file, the selection starts within the whole word then extends to the sentence, paragraph, and so on. In a PHP file, if you start by selecting an argument in a method call,� Navigate to the folder containing the file you want to open as read-only. Instead of clicking the main part of the “Open” button, click the down arrow on the right side of the “Open” button. Select “Read-Only” from the drop-down menu. Your document is opened as read-only, as shown in the image at the beginning of this article. Read

COM Functions - Manual, For further information on COM read the � COM specification. works only with allow_call_time_pass_reference option set to "On" and may not work in Application COM and the Word Document would crash and the PHP script would hang? The default file extension for PHP files is ".php". A PHP file normally contains HTML tags, and some PHP scripting code. Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP function "echo" to output the text "Hello World!" on a web page:

I'm opening a word document from SharePoint site using C#. I've Contributor's rights on the sharepoint site. When the document is opened, it is in editable mode. What I want is to open the document in the Read-Only mode. Is there any way to do this? Thanks. Abhijeet

I have the following vba code: CreateObject("word.Application").Documents _.Open("C:\My Documents\MyPaper.doc").Application.Visible = True and it works fine. What I want to know is this; is there a way to open a document in word but for 'Read Only'? I don't want the users to be able to change any of my document. Thanks in advance, Nancy

Comments
  • i m using linux platform
  • Alright, that's not an option then. But those Word document are created somewhere. The one who creates them can set a password, or even store them as pdf, which is possible in the latest versions of Office.
  • It is easier to edit a word document. For me i replace certain entries making a dynamic word docx. Now I wish to make it read only. Your answer is more like a comment by the way.