最近在处理一个php脚本,需求是给定一个文件夹,文件夹里面有几千个文件夹,每个文件夹里面的内容是图片加word文档,需要读取他们插入到数据库中。
使用了一个composer包叫phpoffice,可以在composer.json中加入require 执行composer install 安装
"require": { "phpoffice/phpword": "^0.16.0" },
$reader = \PhpOffice\PhpWord\IOFactory::createReader();
if ($reader->canRead($filepath)) {
$word = $reader->load($filepath);
$sections = $word->getSections();
foreach ($sections as $section) {
foreach ($section->getElements() as $element) {
if (get_class($element) === 'PhpOffice\PhpWord\Element\TextRun') {
$str = '';
foreach ($element->getElements() as $text) {
$str .= $text->getText();
}
}
}
}
}
然后因为数据太多,正好在laravel的command中