PHP上传word文档并解析,PHP上传Word并读取显示

file("story_img");;

// 4, 判断文件上传的结果

if($result) {

// 上传成功

$imageUrl="./Public/Admin/images/images/";

$zip = new \ZipArchive;

$word=$zip->open('Uploads/'.$result);

if($word==true){

for ($i = 0; $i < $zip->numFiles; $i++) {

/* 用zip压缩读取word中内容 */

$zipElement = $zip->statIndex($i);

/* 读取图片 */

if (preg_match("([^\s]+(\.(?i)(jpg|jpeg|png|gif|bmp))$)", $zipElement['name'])) {

$rand = uniqid();

$file = "{$rand}.png";

$imageFile =$imageUrl. "{$file}";

$im = imagecreatefromstring($zip->getFromIndex($i));

//生成处理过图片,给模糊接口使用

$imWidth = imagesx($im);

$imHeight = imagesy($im);

if ($imWidth > 580) {

$fixWidth = 580; //生成图片的固定宽度

$newHeight = intval($fixWidth / $imWidth * $imHeight);

$newIm = imagecreatetruecolor($fixWidth, $newHeight);

imagecopyresampled($newIm, $im, 0, 0, 0, 0, $fixWidth, $newHeight, $imWidth, $imHeight); //调整图片大小

imagepng($newIm, $imageFile); //生成图片

imagedestroy($im);

imagedestroy($newIm);

} else {

imagepng($im, $imageFile); //生成图片

imagedestroy($im);

}

$a=$this->getFileName($zipElement['name']);

$existImages[$a] = '/Public/Admin/images/images/'.$file;

} else {

//word内容,父级xml

if ($zipElement['name'] == "word/document.xml") {

$content .= $zip->getFromIndex($i);

}

//子级xml

if ($zipElement['name'] == "word/_rels/document.xml.rels") {

$childContent .= $zip->getFromIndex($i);

}

}

}

}

$zip->close();

$zip = null;

$documentContentFix = array('content' => $content, 'childContent' => $childContent, 'existImages' => $existImages);

$content=$this->dealWithContent($documentContentFix);

$pattern = '/

/';

preg_match_all($pattern,$content,$info);

foreach($info as $key => $value){

$title[]=$value[0];

}

$title=$title['1'];

$title=str_replace('>','',$title);

$userinfo=session('user');

$username=$userinfo['username'];

$data=array(

'filename'=>$result,

'imagename'=>$file,

'username'=>$username,

'title'=>$title,

'time'=>time());

M('Fileload')->add($data);

$this->ajaxReturn($content, 1, $_FILES['upfiledata']['name']);

}else {

// 上传失败

echo 333;

die;

}

}

public function dealWithContent($documentContentFix) {

$childContent = $documentContentFix['childContent'];

$existImages = $documentContentFix['existImages'];

$content = $documentContentFix['content'];

$search = array();

$replace = array();

//替换中出现图片位置

preg_match_all('//', $childContent, $matchs);

$keys = $matchs['1']; //id里面内容

$values = $matchs['2']; //word中资源位置及名称

foreach ($keys as $key => $imageKey) {

$imageArrayKey = $this->getFileName($values[$key]);

//判断获取到的资源是否为图片,并且存在

if (array_key_exists($key, $values) && array_key_exists($imageArrayKey, $existImages)) {

$search[] = "

1305895834126159872.htm{$imageKey}";

$replace[] = "

%7B%24existImages%5B%24imageArrayKey%5D%7D";

}

}

//提取xml正文

$content = preg_match("/(.*?)/", $content, $matches);

$content = $matches['1'];

$content = preg_replace('/.*?/', "", $content);

$content = preg_replace('/.*?/', "", $content);

$content = preg_replace('/|/', "", $content);

$content = preg_replace('/.*?/', "", $content);

$content = preg_replace('/.*?/', "", $content);

$content = preg_replace('/()?(

)?/', "", $content);

$content = preg_replace('/()?(

)?/', "\r\n", $content);

$content = preg_replace('//', "\r\n", $content);

$content = preg_replace('//', "\r\n", $content);

$content = preg_replace('/

/', "

", $content);

$content = preg_replace('/

/', "

", $content);

$content = preg_replace('//', "", $content);

$content = preg_replace('//', "", $content);

$content = preg_replace('//', "\r\n", $content);

$content = preg_replace('/.*?/', "", $content);

$content = preg_replace('/.*?/', "

1305895834126159872.htm$1", $content);

$content = preg_replace('/.*?/', "", $content);

$content = preg_replace('//', "", $content);

//var_dump($search);die;

return str_replace($search, $replace, $content); //替换图片位置

}

public function getFileName($file, $md5 = TRUE) {

$fileName = basename($file);

return $md5 ? md5($fileName) : $fileName;

}

public function ajaxReturn($msg, $status = 0, $name = '') {

echo json_encode(array('data' => $msg, 'status' => $status, 'name' => $name));

exit();

}

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved.

京ICP备09083238号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值