我是处理文件的新手,所以我尝试使用laravel 4.2打开.doc或.docx文件。然后在我的控制器中使用ckeditor类在textarea中显示它。以下是我访问该文件的方法:
$userDoc = "uploads/docs/" . $fname;
$fileHandle = fopen($userDoc, "r");
$line = @fread($fileHandle, filesize($userDoc));
$lines = explode(chr(0x0D),$line);
$opendoc = "";
foreach($lines as $thisline)
{
$pos = strpos($thisline, chr(0x00));
if (($pos !== FALSE)||(strlen($thisline)==0))
{
} else {
$opendoc .= $thisline." ";
}
}
$opendoc =preg_replace("/[^a-zA-Z0-9\s\,\.\-\n\r\t@\/\_\(\)]/","",$opendoc);
return View::make('dmy.open_doc' , compact('title', 'smpl' , 'opendoc'));
然后我在我的视图中使用它:
{{ Form::open(array('url' => 'spot', 'method' => 'post')) }}
@foreach($smpl as $list)
@endforeach
<p>{{ Form::text('fid', $list->fileid, array('style' => 'form-control')) }}</p>
<textarea class="ckeditor" name="editor" id = "stuff">
{{ $opendoc }}
</textarea>
{{ Form::submit('Go', array('class' => 'btn btn-primary')) }}
{{ Form::close()}}
不幸的是,我得到的东西只是随机字符,但我能够读取文本文件。有关如何正确做到这一点的任何想法?非常感谢你!