如何使用PHP将最常用的文件(pdf,doc,docx,xl​​s,...)显示到浏览器中?

I need to display a (pdf,doc,docx,txt) documents inside an php web form with the ability to select a part of the document content and do some processing on it in the right click (like saving the selected content to DB)?

I try this way :-

<iframe src="http://docs.google.com/viewer?url=<?=urlencode($docx)?>&embedded=true" width="600" height="780" style="border: none;"></iframe>

But is show me as html

Two things you can do:

  1. Just link to the .pdf/etc. location on your server

  2. Save the image of it as a .jpg and display that then put a link to download the pdf/whatever

PHP can set the header information for a file and so you can display anything as file.php, loading the file.php you can then specify what it should be handled as- for example you can generate an image and output it as a jpeg file :

header('Content-Type: image/jpeg');
$img = LoadJpeg('bogus.image');
imagejpeg($img);
imagedestroy($img);

from http://php.net/manual/en/function.imagecreatefromjpeg.php example.

Using a similar method for setting the header content-type you can output PDF contents or DOC or whatever, by setting the header to the correct file type and loading the contents of the file with PHPs file_get_contents() ( http://php.net/manual/en/function.file-get-contents.php ) function, and outputting that to the browser / iframe.