dongyihao9887 2014-12-07 22:50
浏览 58
已采纳

上传docx文件并使用php将内容和图像存储到DB中

I am trying to upload one docx file containing some contents as well as images. I want to store the file's content and images should be stored into Database. I am using php and mysql. I have trying like:

<?php

/*Name of the document file*/
$document = 'file.docx';

function get_string_between($string, $start, $end) {
        $string = " " . $string;
        $ini = strpos($string, $start);
        if ($ini == 0)
            return "";
        $ini += strlen($start);
        $len = strpos($string, $end, $ini) - $ini;
        return substr($string, $ini, $len);
    }

function read_file_docx($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);

    echo $im = get_string_between($content,"descr=","/>");
    $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
//    $content = str_replace('</w:r></w:p>', "
", $content);
    //$content = str_replace('</w:r></w:p>', "-viavitae-", $content);
  //  $content = str_replace('-viavitae-', $im, $content);
//    $striped_content = strip_tags($content);
    $striped_content = ($content);
//    echo get_string_between($striped_content,"descr=","/>");
    return $striped_content;
}

$content = read_file_docx($document);
if($content !== false) {

    echo nl2br($content);
}
else {
    echo 'Couldn\'t the file. Please check that file.';
}

展开全部

  • 写回答

1条回答 默认 最新

  • dongliu0823 2014-12-08 08:05
    关注

    You can already get the $content using your code.

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

    Now all you need to do is to parse the content as an XML file:

    You need to get all 'w:p' elements (which are paragraphs), get their text (everything that is inside a 'w:t' tag), and join all those blocks together with ' ' to create the paragraphs.

    For the images, you can get them in the folder /word/media/*

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部