【问题标题】:Can we Display the contents of uploaded file in website? [duplicate]我们可以在网站上显示上传文件的内容吗? [复制]
【发布时间】:2015-06-30 12:47:28
【问题描述】:

我的问题是我正在上传一个文件(.pdf/.doc)并将其保存在一个文件夹中,并使用 php 将文件的路径存储在 mysql 中。

在下一个表单中,我想使用 php 显示上传文件的内容。

【问题讨论】:

  • 这是 doc 文件的链接 stackoverflow.com/questions/5334301/… 和 pdf 的链接 stackoverflow.com/questions/18393665/… 您所要做的就是从 sql 中放入正确的文件
  • 我能知道为什么我得到 -ve 指向我的问题吗?请删除 -ve 并建议我更好的提问方式。谢谢
  • 我不是对您投反对票的人,但作为一个建议,我可以建议您:输入示例代码,到目前为止您尝试了什么以及您的预期输出。请花时间阅读此stackoverflow.com/help/how-to-ask
  • 您的问题没有显示任何研究工作。

标签: php


【解决方案1】:

是的,你可以这样做

特别是如果你想在浏览器中显示 pdf,你需要这样的东西。

<?php
 $file = 'path/to/PDF/file.pdf';
 $filename = 'filename.pdf';
 header('Content-type: application/pdf');
 header('Content-Disposition: inline; filename="' . $filename . '"');
 header('Content-Transfer-Encoding: binary');
 header('Accept-Ranges: bytes');
@readfile($file);
?>  

对于word文件,你需要做一些工作

   function readDocs($filename){

            $striped_content = '';
            $content = '';

            if(!$filename || !file_exists($filename)) return false;

            $zip = zip_open($filename);

            if (!$zip || is_numeric($zip)) return false;

            while ($zip_entry = zip_read($zip)) {

                if (zip_entry_open($zip, $zip_entry) == FALSE) continue;

                if (zip_entry_name($zip_entry) != "word/document.xml") continue;

                $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));

                zip_entry_close($zip_entry);
            }// end while

            zip_close($zip);


            $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
            $content = str_replace('</w:r></w:p>', "\r\n", $content);
            $striped_content = strip_tags($content);

            return $striped_content;
        }

        // using function to finally get contents
        $content = readDocs("path/to/the/file");
        if($content !== false) {

            echo nl2br($content);
        }
        else {
            echo "File Not Found";
        }

【讨论】:

  • 非常感谢兄弟,word文档怎么样。
  • 查看更新我已经更新了答案
猜你喜欢
  • 1970-01-01
  • 2018-08-07
  • 2021-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多