doww38701 2014-08-12 11:44
浏览 121
已采纳

使用php编辑上传的.docx文件

I would like to apologize in advance as I'm fairly new to web development and php. I'm currently trying to edit an uploaded .docx file using php and then make it available for download.

As .docx files are zipped folders, my logic is to unzip the uploaded .docx, navigate to the "word/" folder, parse through the XML files I need to edit, edit them with str_replace(); or something similar, re-zip the directory so that it can be opened by MSWord and make it available for downloading. I would prefer to do this with without saving the file to a permanent location, however, I don't know if that's possible.

The problem I am having so far is that I cannot figure out how to navigate through the unzipped folder to get to the .xml files to edit. This is the code so far:

<a href="../index.php">home</a>
<?php
$allowedExts = array("docx");
$temp = explode(".", $_FILES["upload"]["name"]);
$extension = end($temp);

if (($_FILES["upload"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
&& ($_FILES["upload"]["size"] < 20000)
&& in_array($extension, $allowedExts)) {
  if ($_FILES["upload"]["error"] > 0) {
    echo "Error: " . $_FILES["upload"]["error"] . "<br>";
  } else {
    echo "Upload: " . $_FILES["upload"]["name"] . "<br>";
    echo "Type: " . $_FILES["upload"]["type"] . "<br>";
    echo "Size: " . ($_FILES["upload"]["size"] / 1024) . " kB<br>";
    echo "Stored in: " . $_FILES["upload"]["tmp_name"] . "<br>";

    // $document = $_FILES["upload"]["tmp_name"].'/'. $_FILES["upload"]["name"].$extension;
    $document = moveUploadFile($_FILES["upload"]["tmp_name"], $_FILES["upload"]["name"]);
    unzipDocFile($document);

  } 
} else {
  echo "Invalid file <br>";
}


//-------------- functions ------------------//


function unzipDocFile($doc){
    $zip = new ZipArchive();
    if ($zip->open($doc, ZipArchive::CREATE)!==TRUE) {
        exit("cannot open <$doc>
");
    }
    // print_r($zip."<br>");
    // var_dump($zip);
    echo "numFiles: " . $zip->numFiles . "<br>";
    echo "status: " . $zip->status  . "<br>";
    echo "statusSys: " . $zip->statusSys . "<br>";
    echo "filename: " . $zip->filename . "<br>";
    echo "comment: " . $zip->comment . "<br>";
    $zip->close();
}

function moveUploadFile($tmp_name, $name){
    move_uploaded_file($tmp_name, "uploads/" . $name);
    return "uploads/" . $name;
}

function forceDocxDownload($tmp_name){
    header("Content-disposition: attachment; filename=$tmp_name");
    header("Content-type: application/vnd.openxmlformats-    officedocument.wordprocessingml.document");
    readfile("$tmp_name");
}

?>

Any help at all would be much appreciated. Thanks in advance!

  • 写回答

2条回答 默认 最新

  • dongshi949737 2014-08-13 10:12
    关注

    Ok, there is one very simple solution as to how to get access to the contents of the zip folder. I did not realise that one had to extract the contents of the zip folder using extractTo();

    This means that the contents of the .docx file are extracted to tmp_doc/. From there we can manipulate any .xml files that we need to.

    if (isset($_FILES['upload'])) {
        $errors = array();
        $zip = new ZipArchive;
    
        if (end(explode('.', $_FILES["upload"]["name"]))!== 'docx') {
            $errors[] = 'Error: Wrong format';
        }
    
        if($zip->open($_FILES['upload']['tmp_name']) === false){
            $errors[] = 'Failed to open file';
        }
    
        if (empty($errors)) {
            $zip->extractTo('./uploads/tmp_doc');
            $zip->close();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效